B. Jumps

题目:
https://codeforces.com/contest/1455/problem/B
You are standing on the OX-axis at point 0 and you want to move to an integer point x>0.

You can make several jumps. Suppose you’re currently at point y (y may be negative) and jump for the k-th time. You can:

either jump to the point y+k
or jump to the point y−1.
What is the minimum number of jumps you need to reach the point x?

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first and only line of each test case contains the single integer x (1≤x≤106) — the destination point.

Output
For each test case, print the single integer — the minimum number of jumps to reach x. It can be proved that we can reach any integer point x.

题意:
你在X轴的零点位置。第k次你可以有两种操作可以选择,向前跳K个格子或者向左退一个格子。给你终点坐标,问最少多少次可以跳到这个格子。

思路:
思维/
观察可发现,每次如果向前跳的话,相当于加K个格子,如果选择向后退一步的话,实际上是比向前跳少跳了k+1个格子。那也就是说,我们只需要将每次都向前跳所落在的位置计算出来,然后和他给出的终点坐标进行比较。如果刚好相等,说明我们只需要每次向前跳是最优的;如果等于每次向前跳的格子数减一,那我们只能选择跳到这个位置再后退一格(因为就算我们第一步就选择向后跳,那相当于总格数-2,跳不到总格数-1的情况的,所以必须+1才是最优);除此之外,都可以通过中间过程中后退来得到。

代码:

#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double N = 1e6+10;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 100010
ll a[MAXN];
void init()
{
	a[0] = 0;
	for(int i = 1 ; i <= 11000 ; i++)
	{
		a[i] = a[i-1] + i;
	}
}
int main()
{
    int t;
    cin >> t;
    init();
    int l = 11000;
    while(t--)
    {
    	int n;
    	cin >> n;
    	int i;
    	for(i = 1 ; i <= l ; i++)
    	{
    		if(a[i] >= n)
    		{
    			break;
			}
		}
		if(a[i] == n)
		{
			cout << i << endl;
		}
		else if(a[i] == n+1)
		{
			cout << i+1 << endl;
		}
		else
		{
			cout << i << endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值