Jumps (思维)

题目链接: Jumps


大致题意:

你在X轴的零点位置,第k次你可以有两种操作可以选择,向前跳K个格子或者向左退一个格子。给你终点坐标,问最少多少次可以跳到这个格子


解题思路:

思维

每次如果向前跳的话,相当于加K个格子,如果选择向后退一步的话,实际上是比向前跳少跳了k+1个格子

利用前缀和,找到大于等于x的前缀和下标,如果a[index]-1==x,就需要多执行一步-1操作,即index+1为答案,其他所有情况都可以在1到index次操作过程中实现,即答案为index


AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int n, x;
ll a[N];
void init() {
	a[0] = 0;
	for (int i = 1; i < N; ++i) {
		a[i] = a[i - 1] + i;
		if (a[i] >= N) {
			n = i;
			return;
		}
	}
}
int main() {
	init();
	int t; cin >> t;
	while (t--) {
		cin >> x;
		int index = 0;
		for (int i = 1; i <= n; ++i)
			if (a[i] >= x) {
				index = i;
				break;
			}
		if (a[index] - 1 == x)cout << index + 1 << endl;
		else cout << index << endl;
	}
	return 0;
}

END

上一篇:【Codeforces Round #693 (Div. 3) C】Long Jumps


下一篇:Discrete Centrifugal Jumps CodeForces - 1407D 单调栈+dp