AtCoder Regular Contest 070 C - Go Home (构造序列)

题目描述:

 

 

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:

X

Output

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


Sample Input 1 

6

Sample Output 1

3

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


Sample Input 2 

2

Sample Output 2 

2

He 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 

11

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值