題目鏈接:http://acm.hnucm.edu.cn/JudgeOnline/problem.php?id=1349
可以預處理步數內對應的所有步數
無論x爲正還是負數,處理其實是一樣的,我們這裏統一按照正數處理
因爲從正到負(負到正也一樣)至少是要走2步,所以我們二分找最小的a[p]>=x(a[p]爲距離,p爲步數,x爲目標座標)
然後枚舉p使得(a[p] - x) % 2 == 0
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define P pair<int, int>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
static const int N = 44722;
static const int MAX_N = 1e6 + 5;
static const ll Mod = 1e9 + 7;
int a[N];
void prework(){
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
for(int i = 1; i < N; ++i) a[i] = a[i - 1] + i;
}
void solve(){
int T;
scanf("%d", &T);
while(T--){
int x;
scanf("%d", &x);
x = abs(x);
int p = lower_bound(a + 1, a + N + 1, x) - a;
while((a[p] - x) & 1) ++p;
printf("%d\n", p);
}
}
int main(){
prework();
solve();
return 0;
}