cf 1041C双指针

时间:2022-11-20 07:58:37

比赛的时候想着用单调队列做。。。

打完发现其实就是个双指针

/*
构造双指针解决即可
*/ #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 200005
struct sd{int ans,a,id;}x[maxn];
bool cmp1(sd a,sd b){return a.a<b.a;}
bool cmp2(sd a,sd b){return a.id<b.id;}
int main(){
int n,m,d,p=,cnt=;
scanf("%d%d%d",&n,&m,&d);
for(int i=;i<=n;i++) scanf("%d",&x[i].a),x[i].id=i;
sort(x+,x++n,cmp1);
for(int i=;i<=n;i++){
if(!x[i].ans) x[i].ans=++cnt;
while(p<=n && (x[p].ans||x[p].a-x[i].a<=d))
++p;
x[p].ans=x[i].ans;
}
sort(x+,x++n,cmp2);
printf("%d\n",cnt);
for(int i=;i<=n;i++) printf("%d ",x[i].ans);
}