CF1455 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.

Example
input
5
1
2
3
4
5
output
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.

题意
我们能对一个数进行两种操作:
第一种是在第i次操作减一
第二种是在第i次操作加i
现在给你一个数,求最少操作次数能到这个数?

思路
思维题,我们看样例发现
求在1+2+3+…+n哪个区间内,输出i值即可,如果这个数是这个数列和减一的话,则我们要加一次操作,观察可推出。
比如说
1 + 2 + 3 + 4 = 10
9的话只能1 + 2 + 3 + 4 - 1可得,其他区间内的数都能改变其中项为-1实现。

代码

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 1e9+7;
using namespace std;
namespace fastIO {
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 2e6 + 5;
int Case,n;
int f[N]; 
int cnt;
void init(){
	for(int i=0;i<=N;i++){
		int t=(i*i+i)/2;
		if(t>N) break;
		f[cnt++]=t;
	}
}
int main(){
	Case=1;
	init();
	scanf("%d",&Case);
	while(Case--){
		scanf("%d",&n);
		int vis;
		for(int i=1;i<=cnt;i++){
			if(f[i]>=n){
				vis = i;
				break;
			} 
		}
		if(f[vis]-1==n) printf("%d\n",vis+1);
		else printf("%d\n",vis);
	
 	}
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
股价的跳空是指股票价格在连续交易日之间出现大幅度的价格差异。在Python中,可以使用一些库和函数来计算股价的跳空。 引用\[2\]中提到了使用tl.jump.calc_jump_line()函数来获取跳空能量大于阈值的缺口。这个函数可以帮助我们筛选出跳空能量较大的缺口。例如,可以设置一个跳空能量筛选阈值,如2.8,然后使用该函数来计算跳空缺口。 引用\[3\]中提到了tl.jump.calc_jump_line_weight()函数,与上面使用的calc_jump_line()函数不同之处在于它根据时间权重重新计算了跳空能量。这意味着根据时间线性加权的结果,较早的跳空缺口的能量可能会被降低,而较近的跳空缺口的能量仍然保持不变。 因此,如果你想在Python中计算股价的跳空,可以使用这些函数来筛选和计算跳空缺口的能量。 #### 引用[.reference_title] - *1* [lstm预测股票_股票相关性与lstm预测误差](https://blog.csdn.net/weixin_26750511/article/details/109070595)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [第13节 量化技术分析应用](https://blog.csdn.net/weixin_31901801/article/details/114431033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值