Cod sursa(job #2924334)

Utilizator miHai231Mihai Mandragiu miHai231 Data 29 septembrie 2022 20:23:08
Problema Partitie Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <fstream>
#include <algorithm>
using namespace std;
const int NMAX = 1e5 * 3;
unsigned long long sol[NMAX + 5], a[NMAX + 5], n, d, nr, j;
int main()
{
    ifstream in("partitie.in");
    ofstream out("partitie.out");
    in >> n >> d;
    for (int i = 1; i <= n; i++)
        in >> a[i];
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++)
    {
        j = i;
        if (sol[i] == 0)
        {
            sol[i] = ++nr;
        }
        while (j <= n && (sol[j] > 0 || a[j] < a[i] + d))
            j++;
        if (j <= n)
            sol[j] = sol[i];
    }
    out << nr << '\n';
    for (int i = 1; i <= n; i++)
        out << sol[i] << '\n';
    return 0;
}