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 = 1e6 + 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();
cout<<endl;
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;
}
相關文章
- CF1455 D. Sequence and Swaps(模擬)
- B. 酒杯
- B. XOR = Average
- B. Range and Partition
- B. Aleksa and Stack
- B. Charming Meals
- B. AB Flipping
- B. Time Travel
- B. Array Fix
- B. Equal XOR
- B. Composite Coloring
- B. Quasi Binary
- B. Rudolf and 121
- B. Preparing Olympiad
- B. Find The Array
- B. Mashmokh and ACMACM
- B. Two Out of Three
- B. Nezzar and Binary String
- B. Missing Subsequence Sum
- B. Same Parity Summands
- B. Numbers Box(思維)
- B. Navigation System【CF 1320】Navigation
- CCPC Final 2023 B. Periodic Sequence
- 1014 CW 模擬賽 B.旅行
- OJ5-2 B. Shell sort
- B. Negative Prefixes(貪心,構造)
- B.日記和尤拉函式函式
- Codeforces Round #491 (Div. 2) B. Getting an A
- Codeforces Round 928 (Div. 4) B. Vlad and Shapes
- Codeforces Round #676 (Div. 2) B. Putting Bricks in the Wall
- 2020ICPC 江西省賽 B. Apple(思維)APP
- CF 577 B. Modulo Sum 鴿巢原理/01揹包 (*1800)
- The 2024 ICPC Asia EC Regionals Online Contest (II) - Problem B. Mountain BookingAI
- 模擬賽38 B. T形覆蓋 大模擬
- Codeforces Round #537 (Div. 2)B. Average Superhero Gang Power (貪心+暴力)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch SortIntelBAT
- Codeforces Round #689 (Div. 2, based on Zed Code Competition)-B. Find the Spruce(DFS+記憶化搜尋)Zed
- 單調佇列:從一道題說起_2023CCPC廣東省賽B.基站建設佇列