Cod sursa(job #1002672)

Utilizator heracleRadu Muntean heracle Data 28 septembrie 2013 14:52:08
Problema Orase Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <cstdio>
#include <algorithm>

int m,n;

struct oras{
    int d,l;
} p[50020];

bool cmp(oras x, oras y)
{
    return x.d<y.d;
}

int main()
{
    freopen("orase.in","r",stdin);
    freopen("orase.out","w",stdout);

    scanf("%d%d",&m,&n);
    int x;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&p[i].d,&p[i].l);
    }
    std::sort(&p[1],&p[n+1],cmp);

    int st=p[1].l,stang=1;
    for(int i=2;i<=n-1;i++)
    {
        st+=p[i].d-p[i-1].d;
        if(p[i].l>=st)
        {
            st=p[i].l;
            stang=i;
        }
    }
    int dr=p[n].l,drept=n;
    for(int i=n-1; i>stang; i++)
    {
        dr+=p[i+1].d-p[i].d;
        if(p[i].l>=dr)
        {
            dr=p[i].l;
            drept=i;
        }
    }
    printf("%d",p[drept].d-p[stang].d+p[drept].l+p[stang].l);
    return 0;
}