Go Home(UPC)

Go Home(UPC)

There is a kangaroo at coordinate 0 on an infinite number line that runs from left to right, at time 0. During the period between time i−1 and time i, the kangaroo can either stay at his position, or perform a jump of length exactly i to the left or to the right. That is, if his coordinate at time i−1 is x, he can be at coordinate x−i, x or x+i at time i. The kangaroo’s nest is at coordinate X, and he wants to travel to coordinate X as fast as possible. Find the earliest possible time to reach coordinate X.

Constraints
X is an integer.
1≤X≤1e9
*

input

6

output

3

*思路:
情况1:当第i点时恰好走到x,为最佳状态,此时直接输出i;
情况2:当第i点时恰好超过x,当第i-1点时恰好小于x;直接输出i就可;
因为假设第i点走了s(s>x)则我可以在第(s-x)点时不动(少走了(s-x)),恰好补回了那点差距,则可以在第i点直接到x位置;

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int x,i,t;
	cin>>x;
	for(i=1;;i++) {
    
    
		t=(i*(i+1))/2;
		if(t>=x) break;
	}
	cout<<i;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51281451/article/details/113860082
UPC
go