Cod sursă (job #94887)

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

using namespace std;


struct floare{
    int h, z;
};

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

floare v[5000001];

int main(){
	FILE *fin, *fout;
	fin = fopen("lalele.in","r");
	fout = fopen("lalele.out","w");
    
    // Citim
    fscanf(fin, "%d", &n);
    while (i < n) {
        fscanf(fin, "%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(fout, "%d\n", v[top+1]);
            }
        }
    }
    return 0;
}