Cod sursă (job #94851)

Utilizator avatar nita_teddy Teddy Nita nita_teddy IP ascuns
Problemă Lalele (clasele 9-10) Compilator cpp | 0,83 kb
Rundă Tema 9 clasele 9-10 2014/15 Status evaluat
Dată 4 dec. 2014 17:15:34 Scor 54
#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,v[5000001],k[5000001];

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