Cod sursa(job #2714646)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 2 martie 2021 10:14:59
Problema Partitie Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 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];

inline bool cmp(chestie a, chestie b){
    return a.val < b.val;
}

int multimi[NMAX], nr = 1, rez[NMAX];

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);

    int pas = 1;
    for(int i = 1; i <= n; ++i){
        if(vec[i].val - multimi[pas] >= d || i == 1){
            rez[vec[i].wh] = pas;
            multimi[pas] = vec[i].val, ++pas;
            if(pas == nr + 1)
                pas = 1;
        }
        else
            multimi[++nr] = vec[i].val, rez[vec[i].wh] = nr;
    }

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