結果

問題 No.1830 Balanced Majority
ユーザー ぷら
提出日時 2022-02-04 23:02:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,060 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 194,108 KB
最終ジャッジ日時 2025-01-27 19:45:03
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;
    cout << "? " << 1 << endl;
    int S;
    cin >> S;
    cout << "? " << N-1 << endl;
    int s;
    cin >> s;
    S = S-(1-S);
    s = s-(N-1-s);
    if(S == s) {
        cout << "! " << 2 << " " << N-1 << endl;
        return 0;
    }
    int l = 1,r = N;
    while (l+1 < r) {
        int mid = (l+r)/2;
        cout << "? " << mid << endl;
        int a;
        cin >> a;
        a = a-(mid-a);
        if(a == 0) {
            if(mid >= N/2) {
                cout << "! " << 1 << " " << mid << endl;
            }
            else {
                cout << "! " << mid+1 << " " << N << endl;
            }
            return 0;
        }
        if(S < 0 && a < 0) {
            l = mid;
            continue;
        }
        if(S > 0 && a > 0) {
            l = mid;
            continue;
        }
        r = mid;
    }
    if(r >= N/2) {
        cout << "! " << 1 << " " << r << endl;
    }
    else {
        cout << "! " << r+1 << " " << N << endl;
    }
}
0