Cod sursa(job #3125455)

Utilizator Stefan070204Stefan Danda Stefan070204 Data 3 mai 2023 12:07:26
Problema Heavy metal Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 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 > MH;
    SUM += (it->second - it->first);
    MH.first = it->first;
    MH.second = it->second;
    while(it != my_set.end())
    {
        advance(it, 1);
        if(it->first == MH.first)
        {
            if(it->second > MH.second){
               SUM -= MH.second - MH.first;
               MH.first = it->first;
               MH.second = it->second;
               SUM += it->second - it->first;
            }
        }
       else if(it->first < MH.second)
       {
         if(it->second - it->first > MH.second - MH.first)
         {
            SUM -= MH.second - MH.first;
            MH.first = it->first;
            MH.second = it->second;
            SUM += it->second - it->first;  
         }
       }
       else if(it->first >= MH.second)
       {
         SUM += it->second - it->first;
         MH.first = it->first;
         MH.second = it->second;
       }
    }
    fout << SUM;
}
int main()
{
    ios_base::sync_with_stdio(false);
    Task();
    return 0;
}