Cod sursa(job #1437737)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 18 mai 2015 14:45:27
Problema Orase Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

const int NMax = 1000005;

int v[2][NMax];
bool is[NMax];
vector < int > stak;

void add(const int &d,const int &l){
    if(v[0][d] <= l){
        v[1][d] = v[0][d];
        v[0][d] = l;
    } else {
        if(v[1][d] < l){
            v[1][d] = l;
        }
    }
}

int main()
{
    int n,d,l;
    fin >> n >> n;
    for(int i = 1; i <= n; i++){
        fin >> d >> l;
        add(d,l);
        if(!is[d]){
            is[d] = 1;
            stak.push_back(d);
        }
    }
    sort(stak.begin(), stak.end());
    int pos = 0,ans = 0;
    if(v[0][stak[0]] != 0 && v[1][stak[0]] != 0 && v[0][stak[0]] + v[1][stak[0]] > ans){
        ans = v[0][stak[0]] + v[1][stak[0]];
        pos = 0;
    }
    int sum;
    for(int i = 1; i < stak.size(); i++){
        sum = v[0][stak[pos]] + v[0][stak[i]] + (stak[i] - stak[pos]);
        if(sum > ans){
            ans = sum;
        }
        sum -= v[0][stak[i]];
        if(max(sum, v[0][stak[i]]) == v[0][stak[i]]){
            pos = i;
        }
        if(v[0][stak[i]] != 0 && v[1][stak[i]] != 0 && v[0][stak[i]] + v[1][stak[i]] > ans){
            ans = v[0][stak[i]] + v[1][stak[i]];
            pos = i;
        }
    }
    fout << ans;
    return 0;
}