Pagini recente » Cod sursa (job #347281) | Cod sursa (job #2748450) | Cod sursa (job #2403008) | Cod sursa (job #921771) | Cod sursa (job #2508444)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("partitie.in");
ofstream cout("partitie.out");
int p[300005], cap[300005];
struct poz{
int first;
int second;
};
poz v[300005];
bool cmp1(const poz &a, const poz &b)
{
return a.first < b.first;
}
bool cmp2(const poz &a, const poz &b)
{
return a.second < b.second;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, d, i, j, x, cnt=1, okn=0, capmax=0;
cin >> n >> d;
for (i=1; i<=n; i++)
{cin >> x;
v[i].first=x;
v[i].second=i;
}
sort(v+1, v+n+1, cmp1);
p[v[1].second]=1;
cap[1]=v[1].first;
capmax=v[1].first;
for (i=2; i<=n; i++)
{
okn=0;
for (j=1; j<=cnt; j++)
{
if (v[i].first>capmax+d)
{
capmax=v[i].first;
break;
}
if (v[i].first>=cap[j]+d)
{
cap[j]=v[i].first;
if (v[i].first>capmax)
capmax=v[i].first;
p[v[i].second]=j;
okn=1;
break;
}
}
if (okn==0)
{
cnt++;
cap[cnt]=v[i].first;
p[v[i].second]=cnt;
}
}
sort (v+1, v+n+1, cmp2);
cout << cnt << '\n';
for (i=1; i<=n; i++)
cout << p[v[i].second] << '\n';
return 0;
}