Cod sursa(job #3125457)

Utilizator Stefan070204Stefan Danda Stefan070204 Data 3 mai 2023 12:18:06
Problema Heavy metal Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <iterator>
#include <set>

using namespace std;

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

int N;
multiset < pair < int, int > > my_set;

void Task()
{
    fin >> N;
    for(int i = 1; i <= N; ++ i)
    {
        int A, B;
        fin >> A >> B;
        my_set.insert(make_pair(A, B));
    }
    long long SUM = false;
    multiset < pair < int, int > > ::iterator it = my_set.begin();
    pair < int, int > early;
    SUM += (it->second - it->first);
    early.first = it->first;
    early.second = it->second;
    while(it != my_set.end())
    {
        advance(it, 1);
        if(early.first == it->first)
        {
            if(early.second < it->second)
            { 
              SUM += it->second - early.second;
              early.second = it->second;
            }
        }
        else if(early.second > it->first)
        {
            if(it->second - it->first > early.second - early.first)
               {
                SUM -= (early.second - early.second), SUM += it->second - it->first;
                early.first = it->first;
                early.second = it->second;
               }
        }
        else
        {
            SUM += it->second - it->first;
            early.first = it->first;
            early.second = it->second;
        }
    }
    fout << SUM;
}
int main()
{
    ios_base::sync_with_stdio(false);
    Task();
    return 0;
}