Cod sursă (job #94847)

Utilizator avatar nita_teddy Teddy Nita nita_teddy IP ascuns
Problemă Lalele (clasele 9-10) Compilator cpp | 0,90 kb
Rundă Tema 9 clasele 9-10 2014/15 Status evaluat
Dată 4 dec. 2014 16:55:17 Scor 50
#include <cstdio>
#include <algorithm>

using namespace std;

FILE*f=fopen("lalele.in","r");
FILE*h=fopen("lalele.out","w");

int n,x,y,top,zi,i;

struct floare{
    int h,z;
};

bool mod(floare a,floare b){
    return a.h<b.h;
}

floare v[5000001];

int main(){
    fscanf(f,"%d",&n);
    while ( i<n ){
        fscanf(f,"%d%d",&x,&y);
        i+=x;
        if ( y!=0 ){
            for ( int j=1;j<=x;++j ){
                v[++top].h=y;
                v[top].z=++zi;
            }
        }
        else{
            for ( int j=1;j<=x;++j ){
                ++zi;
                for ( int l=1;l<=top;++l ){
                    v[l].h+=zi-v[l].z;
                    v[l].z=zi;
                }
                nth_element(v+1,v+top/2+1,v+top+1,mod);
                top=top/2;
                fprintf(h,"%d\n",v[top+1]);
            }
        }
    }
    return 0;
}