CF 1455B Jumps
题意:
给定 x,从0跳到x的最小步数
可以两种跳
1: 向左跳1步(-1)
2:向右跳k步(+k)k为第几次跳
暴力模拟:
x=6=1+2+3
x=7=1+(-1)+3+4
x=8=(-1)+2+3+4
x=9=1+2+3+4-1
假设s=1+2+....+i=(1+i)*i/2
暴力枚举i,使得s>=x
我们此时得到了一个可行解,i步+(s-x)步
我们注意到,s的元素可以这样减小:j-->-j-1,也就是说可以减小j+1
因为1<=j<=i,所以2<=j+1<=i+1
所以若s-x>=2时,可以转化,答案即为i步
当s-x==1时,在后面一步添一个-1步即可,也就是i+1步
#include<iostream> #include<cmath> using namespace std; int main() { int t; cin >> t; while (t--) { int x; cin >> x; int s = 0, j = 0; while (s < 2 * x) { j++; s = j * (j + 1); } s /= 2; if (s - x == 1)j++; cout << j << "\n"; } return 0; }

浙公网安备 33010602011771号