Cod sursă (job #563596)

Utilizator avatar VladTZY Tiganila Vlad VladTZY IP ascuns
Problemă Lalele (clasele 9-10) Compilator cpp | 0,98 kb
Rundă Arhiva de probleme Status evaluat
Dată 30 aug. 2020 19:58:19 Scor 100
#include <fstream>
#include <algorithm>

#define NMAX 5000005

using namespace std;

ifstream f("lalele.in");
ofstream g("lalele.out");

int n, days, x, total_days;
int day[NMAX], height[NMAX];

int main()
{
    f >> n;

    n = 0;
    while(f >> days >> x)
    {
        if(x != 0)
        {
            for(int i = 1; i <= days; i++)
            {
                height[++n] = x;
                day[n] = ++total_days;
            }
        }
        else
        {
            for(int i = 1; i <= n; i++)
            {
                height[i] = height[i] + total_days - day[i];
                day[i] = total_days;
            }
            for(int i = 1; i <= days; i++)
            {
                total_days++;
                nth_element(height + 1, height + n / 2 + 1, height + n + 1);

                g << height[n / 2 + 1] + total_days - day[n / 2 + 1] << "\n";
                n = n / 2;
            }
        }
    }
    return 0;
}