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.
Example
inputCopy
5
1
2
3
4
5
outputCopy
1
3
2
3
4
Note
In the first test case x=1, so you need only one jump: the 1-st jump from 0 to 0+1=1.
In the second test case x=2. You need at least three jumps:
the 1-st jump from 0 to 0+1=1;
the 2-nd jump from 1 to 1+2=3;
the 3-rd jump from 3 to 3−1=2;
Two jumps are not enough because these are the only possible variants:
the 1-st jump as −1 and the 2-nd one as −1 — you’ll reach 0−1−1=−2;
the 1-st jump as −1 and the 2-nd one as +2 — you’ll reach 0−1+2=1;
the 1-st jump as +1 and the 2-nd one as −1 — you’ll reach 0+1−1=0;
the 1-st jump as +1 and the 2-nd one as +2 — you’ll reach 0+1+2=3;
In the third test case, you need two jumps: the 1-st one as +1 and the 2-nd one as +2, so 0+1+2=3.
In the fourth test case, you need three jumps: the 1-st one as −1, the 2-nd one as +2 and the 3-rd one as +3, so 0−1+2+3=4.
题意
y y y 从 0 0 0 开始移动,第 k k k 次只能移动到 y + k y + k y+k 或者 y − 1 y - 1 y−1,最少走几次能走到点 x x x。
思路
如果我只正着走,位置为 s u m = 0 + 1 + 2 + 3 + 4 + . . . sum = 0 + 1 + 2 + 3 + 4 + ... sum=0+1+2+3+4+... 。
- 那么如果我替换 1 为 - 1 结果为 s u m − 2 sum - 2 sum−2
- 那么如果我替换 2 为 - 1 结果为 s u m − 3 sum - 3 sum−3
- 那么如果我替换 3 为 - 1 结果为 s u m − 4 sum - 4 sum−4
所以 s u m − x > 1 sum - x > 1 sum−x>1 我总能替换掉一个位置使得 s u m = x sum = x sum=x。
如果 s u m − x = 1 sum - x = 1 sum−x=1 那么我只能回退一格,因为最小替换 1 1 1 是回退 2 2 2。
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
int main()
{
int t; cin >> t;
while (t--)
{
int n; cin >> n;
int sum = 0, cnt = 0, ans;
for (int i = 1; sum < n; i++)
{
sum += i;
cnt++;
if (sum == n || sum - n > 1) ans = cnt;
if (sum - n == 1) ans = cnt + 1;
}
cout << ans << endl;
}
return 0;
}