結果

問題 No.850 企業コンテスト2位
ユーザー square1001
提出日時 2019-07-05 21:59:31
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,073 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 78,144 KB
実行使用メモリ 25,476 KB
平均クエリ数 203.36
最終ジャッジ日時 2024-07-16 17:18:33
合計ジャッジ時間 3,078 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int N; vector<int> cand;
int ask(int x, int y) {
	if (x > N) return y;
	if (y > N) return x;
	cout << "? " << x << ' ' << y << endl;
	int res;
	cin >> res;
	return res;
}
int solve(vector<int> v) {
	if (v.size() == 1) return v[0];
	if (v.size() % 2 == 1) {
		int cnt = 1;
		while (find(v.begin(), v.end(), cnt) != v.end()) ++cnt;
		v.push_back(cnt);
	}
	vector<int> pv;
	for (int i = 0; i < v.size(); i += 2) {
		int res = ask(v[i], v[i + 1]);
		if (res == v[i]) pv.push_back(v[i]);
		else pv.push_back(v[i + 1]);
	}
	int ans = solve(pv);
	cand.push_back(v[(find(v.begin(), v.end(), ans) - v.begin()) ^ 1]);
	return ans;
}
int main() {
	cin >> N;
	vector<int> v;
	for (int i = 1; i <= N; ++i) {
		v.push_back(i);
	}
	solve(v);
	sort(cand.begin(), cand.end());
	cand.erase(unique(cand.begin(), cand.end()), cand.end());
	int cur = 0;
	for (int i = 1; i < cand.size(); ++i) {
		int res = ask(cand[cur], cand[i]);
		if (res == cand[i]) cur = i;
	}
	cout << "! " << cand[cur] << endl;
	return 0;
}
0