题目描述:
C - Go Home
Problem Statement
There is a kangaroo at coordinate 00 on an infinite number line that runs from left to right, at time 00. During the period between time i−1i−1 and time ii, the kangaroo can either stay at his position, or perform a jump of length exactly ii to the left or to the right. That is, if his coordinate at time i−1i−1 is xx, he can be at coordinate x−ix−i, xx or x+ix+i at time ii. The kangaroo's nest is at coordinate XX, and he wants to travel to coordinate XX as fast as possible. Find the earliest possible time to reach coordinate XX.
Constraints
- XX is an integer.
- 1≤X≤109
Input
The input is given from Standard Input in the following format:
XOutput
Print the earliest possible time for the kangaroo to reach coordinate X.
Sample Input 1
6Sample Output 1
3The kangaroo can reach his nest at time 33 by jumping to the right three times, which is the earliest possible time.
Sample Input 2
2Sample Output 2
2He can reach his nest at time 22 by staying at his position during the first second, and jumping to the right at the next second.
Sample Input 3
11Sample Output 3
5
题意:
给出一个数X,用序列0,1,2,3,4,,,up去构造X,对于每个数可以加上、减去、或是不选,问最小的up。
思路:
对于任意一个非负数X,>=X最快的方案就是0+1+2+3,,,当和值>=X时,多出来的这一部分肯定在之前的序列中出现过,然后这一部分不选即可。
代码实现:(等差数列求和)
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e5+100;
int arr[N];
int main() {
LL n;
while(cin>>n){
LL tmp=2*n;
tmp=sqrt(tmp);
for(LL i=tmp+1;;i++){
if(i*(i-1)>=2*n){
cout<<i-1<<endl;
break;
}
}
}
return 0;
}
The end;