Cod sursa(job #3154559)

Utilizator susanEmily Susan susan Data 5 octombrie 2023 09:59:37
Problema Heavy metal Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

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

struct event {
    int start, end;

    bool operator<(const event &other) const {
        return (end - start) < (other.end - other.start);
    }
};

int main() {
    multiset<event> v;
    int n;
    fin >> n;

    for (int i = 0; i < n; i++) {
        int start, finish;
        fin >> start >> finish;

        v.insert({start, finish});
    }

    long long total_time = 0;
    while (!v.empty()) {
        event now = *v.rbegin();
        total_time += now.end - now.start;
        v.erase(prev(v.end()));

        erase_if(v, [now](const event x) {
            if (((x.start > now.start) and (x.start < now.end)) || ((x.end > now.start) and (x.end < now.end))) {
                return true;
            }
            return false;
        });

    }

    fout << total_time;

}