Cod sursa(job #2714655)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 2 martie 2021 10:25:06
Problema Partitie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <bits/stdc++.h>

#define NMAX 300005
using namespace std;

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

struct chestie{
    int val, wh;
}vec[NMAX];

int nr = 1, rez[NMAX];
priority_queue<pair<int, int> > pq;

int main()
{
    int n, d;
    fin >> n >> d;

    for(int i = 1; i <= n; ++i)
    {
        fin >> vec[i].val;
        vec[i].wh = i;
    }

    sort(vec + 1, vec + n + 1, cmp);

    pq.push({-vec[1].val, 1});
    rez[vec[1].wh] = 1;
    for(int i = 2; i <= n; ++i){
        if(vec[i].val + pq.top().first >= d){
            auto p = pq.top();
            pq.pop();
            pq.push({-vec[i].val, p.second});
            rez[vec[i].wh] = p.second;
        }
        else {
            ++nr;
            pq.push({-vec[i].val, nr});
            rez[vec[i].wh] = nr;
        }
    }

    fout << nr << '\n';
    for(int i = 1; i <= n; ++i)
        fout << rez[i] << '\n';
    return 0;
}