Cod sursa(job #1203720)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 1 iulie 2014 10:31:01
Problema Partitie Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <cstdio>
#include <algorithm>

#define x first
#define y second
#define NMAX 300007

using namespace std;

pair < int, int > a[NMAX];
int n, d, Sol[NMAX];

void Solve(int poz, int val){
    if(poz <= n){
        int i;
        for(i = poz; i <= n; ++i)
            if(a[i].x - a[poz].x >= d && Sol[a[i].y] == 0)
                break;
        Sol[a[i].y] = val;
        Solve(i, val);
    }
}

int main(){
    freopen("partitie.in", "r", stdin);
    freopen("partitie.out", "w", stdout);
    scanf("%d %d", &n, &d);
    for(int i = 1; i <= n; ++i){
        int A;
        scanf("%d", &A);
        a[i] = make_pair(A, i);
    }
    sort(a + 1, a + n + 1);
    int k = 0;
    for(int j = 1; j <= n; ++j)
        if(Sol[a[j].y] == 0){
            ++k;
            Sol[a[j].y] = k;
            Solve(j, k);
        }
    printf("%d\n", k);
    for(int i = 1; i <= n; ++i)
        printf("%d\n", Sol[i]);
    return 0;
}