Cod sursa(job #1408939)

Utilizator teo.serbanescuTeo Serbanescu teo.serbanescu Data 30 martie 2015 12:31:44
Problema Orase Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream>

using namespace std;

fstream f("orase.in", ios::in);
fstream g("orase.out", ios::out);

struct date
{
    int d, l;
};

const int nmax = 50010;

date a[nmax];

int n, m, sol, maxim, s1, s2;

void read()
{
    int i;
    f >> m >> n;
    for (i = 1; i <= n; i++)
    {
        f >> a[i].d >> a[i].l;
    }
}

void quickSort(int li, int ls)
{
    int i = li, j = ls;
    int mij = (i + j) / 2;
    int aux;
    while (i <= j)
    {
        while (a[i].d < a[mij].d) i++;
        while (a[j].d > a[mij].d) j--;
        if (i <= j)
        {
            aux = a[i].l;
            a[i].l = a[j].l;
            a[j].l = aux;
            aux = a[i].d;
            a[i].d = a[j].d;
            a[j].d = aux;
            i++; j--;
        }
    }
    if (li < j) quickSort(li, j);
    if (i < ls) quickSort(i, ls);
}

void solve()
{
    int i;
    sol = 0;
    maxim = 0;
    for (i = 2; i <= n; i++)
    {
        s1 = sol - a[i - 1].l + a[i].d - a[i - 1].d + a[i].l;
        s2 = a[i - 1].l + a[i].d - a[i - 1].d + a[i].l;
        if (s1 > s2) sol = s1;
                else sol = s2;
        if (sol > maxim) maxim = sol;
    }
    g << maxim;
}

int main()
{
    read();
    quickSort(1, n);
    solve();
    return 0;
}