Cod sursa(job #2142462)

Utilizator antracodsAntracod antracods Data 25 februarie 2018 01:01:10
Problema Orase Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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


struct street_type
{
    int poz,lung;
} v[50005];

bool cmp(street_type a,street_type b)
{
    return a.poz<b.poz;
}

int main()
{
    long long int sol=0;
    int m,n;
    in>>m>>n;
    for(int i=1; i<=n; i++)
    {
        in>>v[i].poz>>v[i].lung;
    }
    sort(v+1,v+n+1,cmp);
    int startl=v[1].lung;
    int startpoz=v[1].poz;
    for(int i=2; i<=n; i++)
    {
        int stopl=v[i].lung;
        int stoppoz=v[i].poz;
        if(startl+stopl+(stoppoz-startpoz)>sol)
                sol=startl+stopl+(stoppoz-startpoz);
        if(stopl>startl+stoppoz-startpoz)
        {
            startpoz=stoppoz;
            startl=stopl;
        }
    }
    out<<sol;
}