Description
Example
input
5
1
2
3
4
5
output
1
3
2
3
4
Solution
可以发现若每次操作都跳i的距离,最终的距离是等差数列之和;
而显然我们不想让-1的操作次数过多,所以尽量先以等差数列跳到第一次 >= x 的位置,设为p
若p == x,则直接出答案,若p > x, 则考虑将之前的若干次跳跃修改为-1操作,使得p -= (p - x)
每修改一次操作 i 为 -1 对答案的贡献为 -(i+1),我们发现贡献是从-2开始连续的,并且只需修改一次就行了
只有p - 1 == x 的时候,无法通过修改之前决策的方法达到x点,所以还需要额外的一次跳跃来 -1
Code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 7;
int p[maxn];
int main() {
int T;scanf("%d",&T);
for(int i = 1;i < maxn;++i) {
p[i] = p[i-1] + i;
}
while(T--) {
int x;scanf("%d",&x);
int pos = lower_bound(p+1,p+1+maxn,x) - p;
int res = pos;
if(p[pos]-1 == x) res++;
printf("%d\n", res);
}
return 0;
}