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;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值