結果

問題 No.1830 Balanced Majority
ユーザー startcppstartcpp
提出日時 2022-02-04 22:24:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 1,372 bytes
コンパイル時間 458 ms
コンパイル使用メモリ 65,060 KB
実行使用メモリ 24,324 KB
平均クエリ数 8.31
最終ジャッジ日時 2023-09-02 05:04:33
合計ジャッジ時間 2,719 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
23,388 KB
testcase_01 AC 23 ms
23,424 KB
testcase_02 AC 24 ms
23,952 KB
testcase_03 AC 26 ms
23,400 KB
testcase_04 AC 24 ms
23,640 KB
testcase_05 AC 23 ms
23,364 KB
testcase_06 AC 23 ms
23,376 KB
testcase_07 AC 25 ms
24,324 KB
testcase_08 AC 24 ms
24,000 KB
testcase_09 AC 25 ms
24,324 KB
testcase_10 AC 24 ms
23,604 KB
testcase_11 AC 24 ms
23,424 KB
testcase_12 AC 26 ms
24,024 KB
testcase_13 AC 26 ms
23,364 KB
testcase_14 AC 26 ms
24,144 KB
testcase_15 AC 26 ms
24,324 KB
testcase_16 AC 25 ms
23,424 KB
testcase_17 AC 26 ms
24,228 KB
testcase_18 AC 24 ms
23,640 KB
testcase_19 AC 23 ms
23,508 KB
testcase_20 AC 23 ms
24,156 KB
testcase_21 AC 27 ms
23,772 KB
testcase_22 AC 26 ms
23,424 KB
testcase_23 AC 26 ms
23,604 KB
testcase_24 AC 27 ms
23,628 KB
testcase_25 AC 25 ms
23,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//表 - 裏を描いてみるが、色々なケースを考えると混乱するので、場合分けによって単純化する。
//f(k) = 上からk枚における表の枚数 - 裏の枚数 とおく。
//まず1枚目とN枚目が異なる向きになる条件とf(1) = f(N - 1)は同値であり、このときは2~N-1枚目を取ればよい。
//1枚目もN枚目も表の場合、f(1) = 1, f(N - 1) = -1であり、
//|f(i) - f(i+1)| = 1が任意のiで成り立つので、f(k) = 0なるk(2~N-2)が存在する。
//これは2分法によって1つ求めることができて、カード1~k or カードk+1~Nのどちらかは条件を満たす。
//1枚目もN枚目も裏の場合は、fの値を反転して解けばよい。
#include <iostream>
using namespace std;

int f(int k) {
	cout << "? " << k << endl;
	cout.flush();
	int s;
	cin >> s;
	return s - (k - s);
}

int main() {
	int n; cin >> n;
	int a = f(1);
	int b = f(n - 1);
	if (a == b) { cout << "! 2 " << n - 1 << endl; return 0; }
	bool hanten = (a < 0);
	
	int l = 1, r = n - 1, mid;	//[l, r)の中にf(k) = 0なるkが存在する
	while (r - l >= 2) {
		mid = (l + r) / 2;
		int res = f(mid) * (hanten ? -1 : 1);
		if (res >= 0) l = mid;
		else r = mid;
	}
	//f(l) = 0.
	if (l >= n / 2) { cout << "! 1 " << l << endl; }
	else { cout << "! " << l + 1 << " " << n << endl; }
	return 0;
}
0