Cod sursă (job #134413)

Utilizator avatar horiainfo Horia T horiainfo IP ascuns
Problemă Lalele (clasele 9-10) Compilator cpp | 1,62 kb
Rundă Arhiva de probleme Status evaluat
Dată 17 mar. 2015 16:37:38 Scor 40
#include <fstream>
#include <cstdio>
#include <vector>
using namespace std;
ofstream fout("lalele.out");
int day,n,d,x;
struct HEAP{int z,h;};
HEAP aux,vmin;
vector<HEAP> heap;

bool H(HEAP h1,HEAP h2)
{
    return (day-h1.z)+h1.h>(day-h2.z)+h2.h;
}
void down()
{
    int tata,fiu;
    tata=1; aux=heap[1];
    while(tata*2<heap.size())
    {
        fiu=tata*2;
        if(fiu+1<heap.size())
            if(H(heap[fiu+1],heap[fiu])==true)
                fiu++;
        if(H(heap[fiu],aux)==true)
            heap[tata]=heap[fiu],tata=fiu;
        else
            break;
    }
    heap[tata]=aux;
}

int elimin(int d)
{
    int jum;
    while(d!=0)
    {
        d--; day++;
        jum=(heap.size()-1)/2;
        if((heap.size()-1)%2==1)
            jum++;
        for(int i=1;i<=jum;i++)
        {
            vmin=heap[1];
            heap[1]=heap[heap.size()-1];
            heap.pop_back();
            down();
        }
        fout<<(vmin.h+(day-vmin.z))<<'\n';
    }
}

void up()
{
    int poz;
    poz=heap.size()-1; aux=heap[heap.size()-1];
    while(poz>1 && H(aux,heap[poz/2])==true)
    {
        heap[poz]=heap[poz/2];
        poz/=2;
    }
    heap[poz]=aux;
}

void adaug(int d,int x)
{
    aux.h=x;
    while(d)
    {
        aux.z=++day;
        heap.push_back(aux);
        up();
        d--;
    }
}

int main()
{
    freopen("lalele.in","r",stdin);
    scanf("%d",&n);
    heap.push_back(aux);
    while(day<n)
    {
        scanf("%d%d",&d,&x);
        if(x==0)
            elimin(d);
        else
            adaug(d,x);
    }
    return 0;
}