Codeforces Round #499 (Div. 2) D. Rocket
問題ページ
Problem - D - Codeforces
考えたこと
- p[i]を把握しないといけない
- y=INFを出力しようと思ったけどだめなのでy=1を出力して確認する
- p[i]さえわかれば二分探索すればよい
- m<=10^9ならクエリ回数30回あれば足りる
- 二分探索を実装して出すと通る
y=1を出力するのに気づけるかどうか、この問題いる?
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define PB push_back const ll INF = (1LL<<60); const int MOD = 1000000007; template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; } template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); } template<class S,class T> ostream &operator <<(ostream& out,const pair<S,T>& a){ out<<'('<<a.first<<','<<a.second<<')'; return out; } template<class T> ostream &operator <<(ostream& out,const vector<T>& a){ out<<'['; REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';} out<<']'; return out; } int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // #define DEBUG int m, n; int x = 10, idx = 0; int pp[30] = {1, -1, 1, -1, -1, -1}; int query(int y) { #ifdef DEBUG cout << "out:" << y << endl; if(y == x) {cout << 0 << endl; idx++; exit(0);} else if(x < y) {cout << -1*pp[idx%n] << endl; idx++; return 0;} else {cout << 1*pp[idx%n] << endl; idx++; return 1;} #else cout << y << endl; int res; cin >> res; if(res == 1) return 1; else if(res == -1) return -1; else exit(0); #endif } signed main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> m >> n; VI p(n); REP(i, n) { if(query(1) == 1) p[i] = 1; else p[i] = 0; } // [lb, ub] int lb = 1, ub = m; REP(i, 60-n) { int mid = (lb+ub)/2; int ret = query(mid); if(p[i%n]==0) ret *= -1; if(ret == 1) lb = mid+1; else ub = mid-1; } return 0; }