题目:
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;
}