Cod sursă (job #96639)

Utilizator avatar Claudiu Dan Claudiu Claudiu IP ascuns
Problemă Lalele (clasele 9-10) Compilator cpp | 0,98 kb
Rundă Tema 9 clasele 9-10 2014/15 Status evaluat
Dată 9 dec. 2014 23:06:32 Scor 0
#include<stdio.h>
#include<algorithm>
struct custom 
{
	int val, zi;
};
int zi = 1;
const int L = 5000002;
custom v[L];
bool cmp (custom a, custom b)
{
	if (a.val + zi - a.zi < b.val + zi - b.zi && a.val != 0 && b.val != 0)
		return true;
	return false;
}
using namespace std;
int main ()
{
	FILE *in, *out;
	in = fopen ("lalele.in", "r");
	out = fopen ("lalele.out", "w");
	int n, lim = 0, zile, operatie, i;
	fscanf(in, "%d", &n);
	while (zi <= n)
	{
		fscanf (in, "%d%d", &zile, &operatie);
		if (operatie != 0)
			for (i = 0; i < zile; i++)
			{
				v[++lim].val = operatie;
				v[lim].zi = zi;
				zi++;
			}
		else
		{
			sort(v, v + lim + 1, cmp); 
			for (i = 1; i <= zile; i++)
			{
				if (lim % 2 == 0)
						fprintf (out, "%d\n", v[lim/2].val + zi - v[lim/2].zi);
				else
				{
					if (v[0].val == 0)
						fprintf (out, "%d\n", v[lim/2 + 1].val + zi - v[lim/2 + 1].zi);
					
				}
				lim = lim / 2 + 1;
				zi++;
			}
		}
	}
	return 0;
}