Pagini recente » Cod sursa (job #374740) | Cod sursa (job #1296375) | Cod sursa (job #1089746) | Statistici Sarmasag Radu Victor (sraduvictor) | Cod sursa (job #271350)
Cod sursa(job #271350)
#include<stdio.h>
#define nmax 100010
long h[nmax], t[nmax], ti[nmax], d[nmax], ln[nmax], n, x, l, nh, tmax, nr, nt;
FILE *f, *g;
void ins(long lana)
{ long k, z;
h[++nh]=lana;
k=nh;
while(h[k/2]<h[k] && k>1)
{ z=h[k]; h[k]=h[k/2]; h[k/2]=z;
k=k/2;
}
}
void del(long k)
{ long z, m;
z=h[k]; h[k]=h[nh]; h[nh]=z;
nh--;
while(( (h[2*k]>h[k] && 2*k<=nh) ||
(h[2*k+1]>h[k] && 2*k+1<=nh) ) && k<nh)
{ if(2*k==nh) m=2*k;
else if(h[2*k]>h[2*k+1]) m=2*k;
else m=2*k+1;
z=h[m]; h[m]=h[k]; h[k]=z;
k=m;
}
}
void insbn(long x, long poz)
{ long i, p, u, mj;
p=1; u=nt;
while(p<=u)
{ mj=(p+u)/2;
if(x<t[mj])
u=mj-1;
else
p=mj+1;
}
for(i=nt+1; i>p; i--)
{ t[i]=t[i-1];
ti[i]=ti[i-1];
}
t[p]=x;
ti[p]=poz;
nt++;
}
int main()
{ long i, j;
f=fopen("lupu.in", "r");
g=fopen("lupu.out", "w");
fscanf(f, "%ld%ld%ld", &n, &x, &l);
for(i=1; i<=n; i++)
{ fscanf(f, "%ld%ld", &d[i], &ln[i]);
if(x-d[i]>=0)
{ insbn((x-d[i])/l+1, i);
if(t[i]>tmax)
tmax=t[i];
}
}
i=n;
for(j=tmax; j>=1; j--)
{ while(t[i]==j)
{ ins(ln[ti[i]]);
i--;
}
if(nh>0)
{ nr+=h[1];
del(1);
}
}
fprintf(g, "%ld\n", nr);
fclose(g);
return 0;
}