Cod sursa(job #843227)

Utilizator elfusFlorin Chirica elfus Data 27 decembrie 2012 16:41:49
Problema Partitie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <stdio.h>
#include <algorithm>
#define NMAX 300300

using namespace std;

int pos[NMAX], x[NMAX], Q[NMAX], sol[NMAX];

inline bool comp(int A, int B)
{
    return x[A] < x[B];
}

int main()
{
    int i, N, D;

    freopen("partitie.in", "r", stdin);
    freopen("partitie.out", "w", stdout);

    scanf("%d%d", &N, &D);
    for (i = 1; i <= N; i ++)
    {
        scanf("%d", &x[i]);
        pos[i] = i;
    }

    sort(pos + 1, pos + N + 1, comp);

    int p = 1, u = 0;
    for (i = 1; i <= N; i ++)
        if (p <= u && x[pos[i]] - x[Q[p]] >= D)
        {
            sol[pos[i]] = sol[Q[p]];
            p ++;
            Q[++ u] = pos[i];
        }
        else
        {
            Q[++ u] = pos[i];
            sol[pos[i]] = u - p + 1;
        }

    printf("%d\n", u - p + 1);
    for (i = 1; i <= N; i ++)
        printf("%d\n", sol[i]);

    return 0;
}