Cod sursa(job #1086670)

Utilizator tudorv96Tudor Varan tudorv96 Data 18 ianuarie 2014 14:10:34
Problema Hotel Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
//Solutie bruta O(P * max nr grupuri de turisti)
#include <fstream>
#include <set>
using namespace std;

ifstream fin ("hotel.in");
ofstream fout ("hotel.out");

set <pair <int, int> > Database;
int n, p;

int query () {
    if (!Database.size())
        return n;
    int MAX = 0;
    for (set <pair <int, int> > :: iterator it = Database.begin(); it != Database.end(); ++it) {
        if (it == Database.begin())
            MAX = max(MAX, it->first-1);
        set <pair <int, int> > :: iterator it2 = it;
        it2++;
        if (it2 != Database.end())
            MAX = max(MAX, it2->first - it->second - 1);
        else
            MAX = max(MAX, n - it->second);
    }
    return MAX;
}

int main() {
    fin >> n >> p;
    while (p--) {
        int type, x, y;
        fin >> type;
        if (type == 3)
            fout << query() << "\n";
        else
            if (type == 1) {
                fin >> x >> y;
                Database.insert (make_pair (x, x + y - 1));
            }
        else
            if (type == 2) {
                fin >> x >> y;
                set <pair <int, int> > :: iterator it = Database.lower_bound (make_pair (x, x + y - 1));
                pair <int, int> N = *it;
                Database.erase (it);
                if (x != N.first)
                    Database.insert (make_pair (N.first, x - 1));
                if (x + y - 1 != N.second)
                    Database.insert (make_pair (x + y, N.second));
            }
    }
}