Pagini recente » Cod sursa (job #31765) | Cod sursa (job #2034532) | Cod sursa (job #2117137) | Cod sursa (job #3237407) | Cod sursa (job #125406)
Cod sursa(job #125406)
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
const int N = 300000;
int n,d;
pair<int,int> a[N];
int m[N];
bool pair_cmp_second ( const pair<int,int> &a, const pair<int,int> &b ) { return a.second < b.second; }
int main() {
freopen("partitie.in","rt",stdin);
freopen("partitie.out","wt",stdout);
scanf("%d %d",&n,&d);
for (int i = 0; i < n; ++i) {
scanf("%d",&a[i].first);
a[i].second = i;
}
sort(a,a+n);
// A walk in the park :)
deque<int> deq;
set<int> mult;
m[0] = 1; deq.push_back(0); mult.insert(1);
int ds = 1;
for (int i = 1; i < n; ++i) {
while (ds != 0 && a[deq.front()].first <= a[i].first-d) {
mult.erase(mult.find(m[deq.front()]));
deq.pop_front();
--ds;
}
if (ds == 0) {
m[i] = 1;
} else {
set<int>::iterator x;
if ((*(mult.begin())) == 1) {
x = mult.end();
--x;
m[i] = (*x)+1;
} else {
x = mult.begin();
m[i] = (*x)-1;
}
}
mult.insert(m[i]);
deq.push_back(i);
++ds;
}
// Back home
for (int i = 0; i < n; ++i) a[i].first = i;
sort(a,a+n,pair_cmp_second);
int max = 0;
for (int i = 0; i < n; ++i)
if (m[i] > max) max = m[i];
printf("%d\n",max);
for (int i = 0; i < n; ++i) {
printf("%d\n",m[a[i].first]);
}
return 0;
}