Cod sursă (job #574478)

Utilizator avatar Mihai_Eduard Mihai Eduard Mihai_Eduard IP ascuns
Problemă Lalele (clasele 9-10) Compilator cpp | 1,39 kb
Rundă Arhiva de probleme Status evaluat
Dată 8 dec. 2020 11:48:56 Scor 30
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>

#define mx 500001

using namespace std;

ifstream fin("lalele.in");
ofstream fout("lalele.out");

int n, nr, ziua[mx], h[mx], total;

int partition(int arr[], int low, int high)
{
    int pivot = arr[high];
    int i = (low - 1);

    for (int j = low; j <= high- 1; j++)
    {
        if (arr[j] <= pivot)
        {
            i++;
            swap(arr[i], arr[j]);
        }
    }
    swap(arr[i + 1], arr[high]);
    return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

int main()
{
    int zile, x;
    fin>>nr;
    while(fin>>zile>>x)
    {
        if(x)
        {
            for(int i=1;i<=zile;i++)
            {
                total++;
                ziua[++n]=total;
                h[n]=x;
            }
        }
        else
        {
            for(int i=1;i<=n;i++)
            {
                h[i]=h[i]+total-ziua[i];
                ziua[i]=total;
            }
            for(int i=1;i<=zile;i++)
            {
                total++;
                quickSort(h,1,n);
                fout<<h[1+n/2]+total-ziua[1+n/2]<<'\n';
                n=n/2;
            }
        }
    }
}