Cod sursa(job #331144)

Utilizator freak93Adrian Budau freak93 Data 12 iulie 2009 20:59:43
Problema Lupul Urias si Rau Scor 12
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<fstream>
#include<algorithm>

using namespace std;

ifstream f("lupu.in");
ofstream g("lupu.out");

const int maxn=100005;

int h[maxn],n,i,j,x,l,maxt,p;

long long s;

struct nod
{
    int d;
    int l;
}
a[maxn];

bool fcomp(nod a,nod b)
{
    return a.d>b.d;
}

void push(int x)
{
    int aux;
    while((x>>1)&&h[x>>1]<h[x])
    {
        aux=h[x];
        h[x]=h[x>>1];
        h[x>>1]=aux;
        x>>=1;
    }
}

void pop(int x)
{
    int aux,y=0;
    while(x!=y)
    {
        y=x;
        if((y<<1)<=p&&h[y<<1]>h[y])
            x=y<<1;
        if((y<<1)+1<=p&&h[(y<<1)+1]>h[y])
            x=(y<<1)+1;
        aux=h[x];
        h[x]=h[y];
        h[y]=aux;
    }
}

int main()
{
    f>>n>>x>>l;
    maxt=-0x3f3f3f3f;
    for(i=1;i<=n;++i)
        f>>a[i].d>>a[i].l,a[i].d=(x-a[i].d+l)/l,maxt=max(maxt,a[i].d);
    sort(a+1,a+n+1,fcomp);
    i=1;
    for(j=maxt;j;--j)
    {
        h[++p]=a[i].l,push(p);
        while(a[i+1].d==j)
            h[++p]=a[++i].l,push(p);
        s+=h[1];
        h[1]=h[p--];
        pop(1);
        if(p==0)
            j=a[i+1].d+1;
    }

    g<<s<<"\n";
    f.close();
    g.close();

    return 0;
}