2020-12-05

Educational Codeforces Round 99 B题题解 (Rated for Div. 2)

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.

题意:

输入一个数字坐标,要求从0坐标变到那个数字坐标,每次可以做两种不同的变化,第一种时在现在数字坐标的基础上加上次数对应的数字,第二种是在现坐标基础上减去1,求变化完成所需的最小次数。

思路:

按照顺序从1开始累计,直到和的结果等于或大于输入坐标为止,每次的累计用一个变量来记录次数,若结果刚好等于所输入的坐标,则直接输出次数;若大于,则求出大于的坐标数,让这个数从累计的最后一位开始累减,直到小于2时,若最后等于一则不能通过改变前面的走法实现,则只能再走一步减一的操作,便需要在次数上加一,反之就不需要加一。

代码:

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n,m;
	cin>>n;
	while(n--){
		cin>>m;
		int sum = 0,b=0;
		for(int i = 1 ; i <=m ; i++){
			sum += i;
			b++;
			if(sum>=m){
				int a = sum - m;
				for(int j = i;j > 1 ; j--){
					if(a>=j) a -= j;
				}
				b += a ;
				cout<<b<<endl;
				break;
			}
		}
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值