Cod sursa(job #481145)

Utilizator CossAlbulescu Cosmina Coss Data 30 august 2010 19:07:23
Problema Orase Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <stdio.h>
#include <algorithm>
using namespace std;

int m, n, i, j, k;
long int dist, Max;

struct strazi
{
    int d;
    int l;
};

struct strazi v[50001];

int cmp (strazi a, strazi b)
{
    if (a.d != b.d)
        return a.d < b.d;
    else
        return a.l < b.l;
}

int main ()
{
    FILE *f = fopen ("orase.in","r");
    FILE *g = fopen ("orase.out","w");
    fscanf (f,"%d %d", &m, &n);
    for (i=1; i<=n; ++i)
        fscanf (f,"%d %d", &v[i].d, &v[i].l);

    sort (v + 1, v + 1 + n, cmp);
    j = v[1].l - v[1].d;
    for (i=2; i<=n; ++i)
    {
        if (Max < v[i].l + v[i].d + j)
            Max = v[i].l + v[i].d + j;
        if (j < v[i].l - v[i].d)
            j = v[i].l - v[i].d;
    }

    fprintf (g, "%ld\n", Max);

    fclose (g);
    fclose (f);
    return 0;
}