Pagini recente » Cod sursa (job #1300050) | Cod sursa (job #233088) | Cod sursa (job #586964) | Cod sursa (job #2798396) | Cod sursa (job #162777)
Cod sursa(job #162777)
#include<stdio.h>
#include<stdlib.h>
struct interval
{
int a,b;
};
//bool c[];
int best[1000000];
interval v[100000];
//bool c[1000000001];
//bool *c;
int compar(const void*p,const void*q){
interval *pp=(interval*)p, *qq=(interval*)q;
interval u=*pp,w=*qq;
if(u.b<w.b)
return -1;
if(u.b>w.b)
return 1;
return u.a-w.a;
}
int main()
{
freopen("heavymetal.in","r",stdin);
freopen("heavymetal.out","w",stdout);
int n,i,x,j=0,aux;
scanf("%d",&n);
//c=new bool[n+1];
for(i=0; i<n; i++)
{
scanf("%d%d",&v[i].a,&v[i].b);
//c[v[i].a]=c[v[i].b]=true;
}
qsort(v,n,sizeof(v[0]),compar);
/*for(i=0; i<n; i++)
{
printf("%d %d\n",v[i].a,v[i].b);
}*/
x=v[n-1].b;
for(i=1; i<=x; i++)
{
//if(c[i]==true)
//{
best[i]=best[i-1];
while(i==v[j].b)
{
aux=best[v[j].a]+v[j].b-v[j].a;
if(aux>best[i])
best[i]=aux;
j++;
}
/*if(best[i]>max)
max=best[i];*/
//}
}
printf("%d\n",best[x]);
return 0;
}