Cod sursa(job #612871)

Utilizator crushackPopescu Silviu crushack Data 11 septembrie 2011 23:03:49
Problema Orase Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <stdio.h>
#include <deque>
#include <algorithm>
#define NMax 50010
using namespace std;

const char IN[]="orase.in",OUT[]="orase.out";

struct oras{
    int d,l;
    bool operator<(oras const &b) const{
        return d<b.d;
    }
} a[NMax];

int N,M,S;
deque<int> d;

void add(int x)
{
    while ( !d.empty() && x>d.front()) d.pop_front();
    d.push_back(x);
}

int main()
{
    int i,R=-1;
    freopen(IN,"r",stdin);
    scanf("%d%d",&M,&N);
    for (i=1;i<=N;++i) scanf("%d%d",&a[i].d,&a[i].l);
    fclose(stdin);

    sort(a+1,a+N+1);

    for (i=1;i<=N;++i)
    {
        S+=a[i].d-a[i-1].d;
        if (!d.empty()) R=a[i].l+S+d.front();
        add(a[i].l-S);
    }

    freopen(OUT,"w",stdout);
    printf("%d\n",R);
    fclose(stdout);

    return 0;
}