题面传送门
这道题可以任意
±
x
\pm x
±x,所以可以
%
x
\%x
%x考虑。将每一个数进来后
%
x
\%x
%x并加入一个数组f中,设
f
i
f_i
fi为
%
x
=
i
\%x=i
%x=i的数有几个。同时有一个指针为
h
e
a
d
head
head。
若
h
e
a
d
<
x
head<x
head<x,那么
f
h
e
a
d
>
0
f_{head}>0
fhead>0,
h
e
a
d
+
+
head++
head++
如果
h
e
a
d
≥
x
head\geq x
head≥x,那么就要先满足小的,再来管大的。小的总共有
⌊
h
e
a
d
x
⌋
\left\lfloor\dfrac{head}{x}\right\rfloor
⌊xhead⌋ 。再加上大的一个总共是
⌊
h
e
a
d
x
⌋
+
1
\left\lfloor\dfrac{head}{x}\right\rfloor+1
⌊xhead⌋+1个。
把两个式子整合一下,得到
f
h
e
a
d
>
⌊
h
e
a
d
x
⌋
,
h
e
a
d
+
+
f_{head} >\left\lfloor\dfrac{head}{x}\right\rfloor,head++
fhead>⌊xhead⌋,head++。
我看题解里好像没有比我短的。
代码实现:
#include<cstdio>
using namespace std;
int n,x,a,f[400039],ans,head;
inline void read(int &x){
char s=getchar();x=0;
while(s<'0'||s>'9') s=getchar();
while(s>='0'&&s<='9') x=(x<<3)+(x<<1)+(s^48),s=getchar();
}
inline void print(int x){
if(x>9) print(x/10);
putchar(x%10+48);
}
int main(){
register int i;
scanf("%d%d",&n,&x);
for(i=1;i<=n;i++){
read(a);
f[a%x]++;
while(f[head%x]>head/x) head++;
print(head);putchar('\n');
}
return 0;
}
注意加读优输优。