Cod sursa(job #2406573)

Utilizator TeoDiDiaconescu Teodora TeoDi Data 15 aprilie 2019 21:33:38
Problema Partitie Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <bits/stdc++.h>
#include <set>
#define Nax 300001
int n,d;

std::pair<int,int>v[Nax];
int poz[Nax];
int f[Nax][2];
int nax;
using namespace std;
//ifstream fin("partitie.in");
//ofstream fout("partitie.out");

int main()
{
    freopen("partitie.in", "r", stdin);
	freopen("partitie.out", "w", stdout);
    scanf("%d %d\n", &n, &d);
    for(int i=1;i<=n;i++) {scanf("%d\n", &v[i].first); v[i].second=i;}
    sort(v+1,v+n+1);
    f[1][0]=1;
    f[1][1]=1;
    poz[v[1].second]=1;
    nax=1;
    for(int i=2;i<=n;i++)
    {
        bool ok=0;
        for(int j=1;j<=nax && !ok;j++)
        {
            if(v[i].first-v[f[j][1]].first>=d)
            {
                ok=1;
                f[j][0]++;
                f[j][1]=i;
                poz[v[i].second]=j;
            }
        }
        if(!ok)
        {
            nax++;
            f[nax][0]=1;
            f[nax][1]=i;
            poz[v[i].second]=nax;
        }
    }
    printf("%d\n",nax);
    for(int i=1;i<=n;i++)
        printf("%d\n",poz[i]);
    return 0;
}