Cod sursa(job #1775709)

Utilizator vladboss2323Ciorica Vlad vladboss2323 Data 10 octombrie 2016 17:20:06
Problema Orase Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

const int N = 50001;

struct oras
{
    int d, l;
};

oras v[N];

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

int distanta(int i, int j) {
    return v[j].d - v[i].d + v[i].l + v[j].l;
}

int main()
{
    ifstream in("orase.in");
    ofstream out("orase.out");
    int lmax,dc,n;
    in >> lmax >> n;
    for (int i = 0; i < n; i++) {
        in >> v[i].d >> v[i].l;
    }
    sort(v, v + n, cmp);
    int ultim = 0, dmax = distanta(0, 1);
    for (int i = 2; i < n; i++) {
        dc = distanta(ultim, i);
        if (distanta (i - 1, i) > dc) {
            dc = distanta(i - 1, i);
            ultim = i - 1;
        }
        if (dc > dmax)
            dmax = dc;
    }
    out<<dmax;
    in.close();
    out.close();
    return 0;
}