Go Home (思维)

Go Home(思维)

description

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≤109

Input

The input is given from Standard Input in the following format:
X

output

Print the earliest possible time for the kangaroo to reach coordinate X.

sample input

6

sample output

3

tip

The kangaroo can reach his nest at time 3 by jumping to the right three times, which is the earliest possible time.

题解:

袋鼠在第i次跳i个单位,每次可以选择向左跳、向右跳、不跳,问最少次跳到X。

如果一直向右跳,考虑两种情况:1.恰好在第i次跳到X,这是我们最想遇到的情况。2.第i次跳过X,第i-1次没跳到X,假设在第i次跳到X+n,易知n<X,我们可以选择在第n次不跳,在第i次正好到X

#include<bits/stdc++.h>
using namespace std;
const int maxn = 44722;
int a[maxn];
int main()
{
    int n,ans;
    for(int i=1;i<maxn;i++){
        a[i] = a[i-1]+i;
    }
    scanf("%d",&n);
    ans = lower_bound(a,a+maxn,n)-a;
    printf("%d\n",ans);
    return 0;
}



猜你喜欢

转载自blog.csdn.net/h_usky/article/details/80668134