Cod sursa(job #2802622)

Utilizator iulianarsenoiuArsenoiu Iulian iulianarsenoiu Data 18 noiembrie 2021 15:51:33
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.5 kb
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pii;

int n,k;
int t[10005],p[10005],lap[10005];

pii h[10005];
int Heap = 0;

const int bsize = (1<<16);

char buffer[bsize];

int P = 0;

void ReadInt(int &nr)
{
    nr = 0;
    while(!isdigit(buffer[P]))
    {
        if(P==bsize-1)
        {
            fread(buffer,1,bsize,stdin);
            P = 0;
        }
        else
        {
            ++P;
        }
    }
    while(isdigit(buffer[P]))
    {
        nr = nr * 10 + buffer[P] - '0';
        if(P==bsize-1)
        {
            fread(buffer,1,bsize,stdin);
            P = 0;
        }
        else
        {
            ++P;
        }
    }
}

void HeapUp(int nod)
{
    if(nod==1)
    {
        return;
    }
    if(h[nod/2].first>h[nod].first)
    {
        swap(h[nod/2],h[nod]);
        HeapUp(nod/2);
    }
}

void HeapDown(int nod)
{
    if(nod*2+1<=Heap)
    {
        if(h[nod*2].first<h[nod*2+1].first)
        {
            if(h[nod].first>h[nod*2].first)
            {
                swap(h[nod],h[nod*2]);
                HeapDown(nod*2);
            }
        }
        else
        {
            if(h[nod].first>h[nod*2+1].first)
            {
                swap(h[nod],h[nod*2+1]);
                HeapDown(nod*2+1);
            }
        }
        return;
    }
    if(nod*2<=Heap)
    {
        if(h[nod].first>h[nod*2].first)
        {
            swap(h[nod],h[nod*2]);
            HeapDown(nod*2);
        }
    }
}

void Push(pair<int,int> val)
{
    ++Heap;
    h[Heap] = val;
    HeapUp(Heap);
}

void Pop()
{
    swap(h[1],h[Heap]);
    h[Heap] = {0,0};
    --Heap;
    HeapDown(1);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("competitie.in","r",stdin);
    freopen("competitie.out","w",stdout);
    ReadInt(n);
    ReadInt(k);
    for(int i=1;i<=n;i++)
    {
        ReadInt(t[i]);
        ReadInt(p[i]);
        cin>>t[i]>>p[i];
        Push({t[i],i});
    }
    int Max = 0;
    while(Heap)
    {
        int timp = h[1].first;
        int cnt = 0;
        while(Heap && h[1].first==timp)
        {
            int poz = h[1].second;
            Pop();
            ++lap[poz];
            ++cnt;
            if(lap[poz]==k)
            {
                continue;
            }
            Push({timp+t[poz]+lap[poz]%p[poz],poz});
        }
        Max = max(Max,cnt);
    }
    cout<<Max<<'\n';
    return 0;
}