Pagini recente » Cod sursa (job #1214243) | Cod sursa (job #2113070) | Cod sursa (job #1140127) | Cod sursa (job #224467) | Cod sursa (job #1494854)
#include <cstdio>
#include <algorithm>
#define MAXN 300000
typedef struct{
int v, t;
}mycreation;
mycreation e[MAXN], v[MAXN];
int r[MAXN], h;
bool cmp(const mycreation a, const mycreation b){
return a.v<b.v;
}
inline int fiust(int p){
return 2*p+1;
}
inline int fiudr(int p){
return 2*p+2;
}
inline void swap(int x, int y){
mycreation aux=e[x];
e[x]=e[y];
e[y]=aux;
}
inline void coborare(int p){
int q, f=1;
while((f==1)&&(fiust(p)<h)){
q=fiust(p);
if((fiudr(p)<h)&&(e[fiudr(p)].v<e[q].v)){
q=fiudr(p);
}
if(e[q].v<e[p].v){
swap(p, q);
p=q;
}else{
f=0;
}
}
}
int main(){
int n, i, k;
FILE *fin, *fout;
fin=fopen("partitie.in", "r");
fout=fopen("partitie.out", "w");
fscanf(fin, "%d%d", &n, &k);
for(i=0; i<n; i++){
fscanf(fin, "%d", &v[i].v);
v[i].t=i;
}
std::sort(v, v+n, cmp);
h=1;
e[0].v=v[0].v;
e[0].t=1;
r[v[0].t]=1;
for(i=1; i<n; i++){
if(v[i].v-e[0].v<k){
e[h].v=v[i].v;
h++;
e[h-1].t=h;
r[v[i].t]=h;
}else{
r[v[i].t]=e[0].t;
e[0].v=v[i].v;
coborare(0);
}
}
fprintf(fout, "%d\n", h);
for(i=0; i<n; i++){
fprintf(fout, "%d\n", r[i]);
}
fclose(fin);
fclose(fout);
return 0;
}