Pagini recente » Cod sursa (job #2731084) | Cod sursa (job #2249246) | Cod sursa (job #317599) | Cod sursa (job #1306450) | Cod sursa (job #125170)
Cod sursa(job #125170)
#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;
int max = 0;
m[0] = 1; deq.push_back(0); mult.insert(1);
for (int i = 1; i < n; ++i) {
while (!deq.empty() && a[deq.front()].first <= a[i].first-d) {
mult.erase(mult.find(m[deq.front()]));
deq.pop_front();
}
if (deq.size() == 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);
if (deq.size() > max) max = deq.size();
}
// Back home
for (int i = 0; i < n; ++i) a[i].first = i;
sort(a,a+n,pair_cmp_second);
printf("%d\n",max);
for (int i = 0; i < n; ++i) {
printf("%d\n",m[a[i].first]);
}
return 0;
}