Pagini recente » Cod sursa (job #2920448) | Cod sursa (job #1234906) | Cod sursa (job #3127812) | Cod sursa (job #1023899) | Cod sursa (job #875929)
Cod sursa(job #875929)
#include<stdio.h>
#include<algorithm>
using namespace std;
struct oras
{
int x;
int lungime;
};
oras v[50005];
bool comp(oras a,oras b)
{
if(a.x<b.x)
return true;
return false;
}
int modul(int x)
{
if(x<0)
return -x;
return x;
}
int m,n;
int main()
{
int i;
FILE *f=fopen("orase.in","r");
FILE *g=fopen("orase.out","w");
fscanf(f,"%d",&m);
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
{
fscanf(f,"%d",&v[i].x);
fscanf(f,"%d",&v[i].lungime);
}
sort(v+1,v+n,comp);
oras maxim;
int poz1,poz2;
maxim.x=v[1].x;
maxim.lungime=v[1].lungime;
poz1=1;
for(i=2;i<=n;i++)
{
if((v[i].x-maxim.x+maxim.lungime)<v[i].lungime)
{
maxim.x=v[i].x;
maxim.lungime=v[i].lungime;
poz1=i;
}
}
maxim.x=v[n].x;
maxim.lungime=v[n].lungime;
poz2=n;
for(i=n;i>=1;i--)
{
if(v[i].lungime>(maxim.x-v[i].x+maxim.lungime))
{
maxim.x=v[i].x;
maxim.lungime=v[i].lungime;
poz2=i;
}
}
if(poz1!=poz2)
{
fprintf(g,"%d",v[poz1].lungime+v[poz2].lungime+modul(v[poz1].x-v[poz2].x));
}
else
{
int d=0,curent;
for(i=1;i<poz1;i++)
{
curent=v[i].lungime+v[poz1].lungime+modul(v[poz1].x-v[i].x);
if(curent>d)
d=curent;
}
for(i=poz1+1;i<=n;i++)
{
curent=v[i].lungime+v[poz1].lungime+modul(v[poz1].x-v[i].x);
if(curent>d)
d=curent;
}
fprintf(g,"%d",d);
}
}