Cod sursa(job #2964254)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 12 ianuarie 2023 18:38:11
Problema Partitie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("partitie.in");
ofstream g("partitie.out");
pair<int, int> A[300005];
int n, k, sol[300005], nr, j;
int main()
{
    f >> n >> k;
    for(int i = 1; i <= n; i ++)
    {
        f >> A[i].first;
        A[i].second = i;
    }

    sort(A + 1, A + n + 1);

    for(int i = 1; i <= n; i ++)
    {
        if(sol[A[i].second] == 0)sol[A[i].second] = ++ nr;

        while(j <= n && (sol[A[j].second] > 0 || A[j].first - A[i].first < k))
            j ++;

        if(j <= n)
            sol[A[j].second] = sol[A[i].second];
    }

    g << nr << '\n';
    for(int i = 1; i <= n; i ++)
        g << sol[i] << '\n';

    return 0;
}