Cod sursa(job #2501216)

Utilizator ardutgamerAndrei Bancila ardutgamer Data 29 noiembrie 2019 11:29:02
Problema Partitie Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>

using namespace std;

const int NMAX = 300005;
multiset<int>st;
map<int,int>pos;
int sol[NMAX];

int main()
{
    freopen("partitie.in","r",stdin);
    freopen("partitie.out","w",stdout);
    int x,n,d;
    scanf("%d%d",&n,&d);
    for(int i = 1 ; i <= n ; i++)
    {
        scanf("%d",&x);
        pos[x] = i;
        st.insert(x);
    }
    int cnt = 0;
    while(!st.empty())
    {
        cnt++;
        multiset<int>::iterator it = st.begin();
        while(it != st.end())
        {
            multiset<int>::iterator rit = it;
            it = lower_bound(st.begin(),st.end(),*it+d);
            sol[pos[*rit]] = cnt;
            st.erase(rit);
        }
    }
    printf("%d\n",cnt);
    for(int i = 1 ; i <= n ; i++)
        printf("%d\n",sol[i]);
    return 0;
}