Cod sursa(job #794342)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 6 octombrie 2012 11:03:52
Problema Orase Scor 100
Compilator cpp Status done
Runda dinamica_i Marime 0.76 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

const int dim = 50005;

int N, M;
struct structoras { int d, l; } oras[dim];


int cmp (structoras a, structoras b)
{
	return a.d < b.d;
}

int dist (int o1, int o2)
{
	return oras[o1].l + (oras[o2].d - oras[o1].d) + oras[o2].l;
}

void cit ()
{
	fi >> M >> N;
	for (int i = 0; i < N; i++)
	{
		fi >> oras[i].d >> oras[i].l;
	}
	sort (oras, oras + N, cmp);
}

void rez ()
{
	int best = 0, distmax = 0, d;
	for (int i = 1; i <= N; i++)
	{
		d = dist (best, i);
		if (distmax < d)
			distmax = d;
		
		if (d - oras[i].l < oras[i].l)
			best = i;
	}
	fo << distmax;
}

int main ()
{
	cit ();
	rez ();
	return 0;
}