B. Jumps (simulation)

B. Jumps
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Question meaning:
from 0 00 o'clock to go
firstkkYou can have two choices for k steps:

  1. Go forward kkk steps+k
  2. Go backward 1 11 step-1
    ask you to go tonnHow many steps should be taken at least at n ?

Solution:
In fact, it is a very simple simulation. If you go forward every time, then it is 1 + 2 + 3 + 4..... + x = y 1+2+3+4.....+x=y1+2+3+4.....+x=y Yes= (n + 1) ∗ n / 2 y = (n + 1) * n / 2Y=(n+1)n / 2 .
Then we have to go to y-2, we can add+ 1 +1+ 1 is changed to− 1 -11 .
Go toy − 3 y-3Y3 we can add+ 2 + 2+ 2 is changed to− 1 -11

toy − x + 1 y-x+1Yx+1 we can add+ (x − 2) + (x-2)+(x2 ) to− 1 -11
is actually inyyy minus the number of steps changedzzz and change it to− 1 -11 and subtract1 11 , so we have to subtract2 2 at least2 . Fory − 1 y-1Y1 we can only let him inyyy took a step back.

So we deduce,
(1). If (n + 1) ∗ n / 2 = y (n+1)*n/2=y(n+1)n/2=y then just gonnn step
(2). If(n + 1) ∗ n / 2 = y + 1 (n+1)*n/2=y+1(n+1)n/2=Y+1 Then just gonnn Step Beyond backspace i.e.n + 1 n + 1n+1 step.
(3). Others are actually the same as we analyzednnn steps.

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
ll sum;
ll dp[1000100];
string str;
int main() {
    
    
    int t;
    cin>>t;

    while(t--) {
    
    
        cin>>n;
        int i = 1;
        for(; i * (i + 1) / 2 < n; i ++);
        sum = i * (i + 1) / 2;
        if(sum == n) {
    
    
            cout << i << endl;
        } else if(sum == n + 1) {
    
    
            cout << i + 1 << endl;
        } else cout << i << endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45911397/article/details/110528296