結果

問題 No.1830 Balanced Majority
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2022-02-04 23:20:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 3,379 bytes
コンパイル時間 2,211 ms
コンパイル使用メモリ 200,360 KB
実行使用メモリ 24,348 KB
平均クエリ数 10.88
最終ジャッジ日時 2023-09-02 05:54:21
合計ジャッジ時間 4,462 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
24,324 KB
testcase_01 AC 26 ms
23,676 KB
testcase_02 AC 25 ms
23,544 KB
testcase_03 AC 25 ms
23,544 KB
testcase_04 AC 25 ms
23,652 KB
testcase_05 AC 25 ms
23,412 KB
testcase_06 AC 24 ms
23,400 KB
testcase_07 AC 27 ms
23,700 KB
testcase_08 AC 27 ms
24,288 KB
testcase_09 AC 27 ms
24,336 KB
testcase_10 AC 28 ms
23,436 KB
testcase_11 AC 27 ms
23,424 KB
testcase_12 AC 27 ms
24,060 KB
testcase_13 AC 28 ms
23,544 KB
testcase_14 AC 28 ms
24,312 KB
testcase_15 AC 28 ms
23,424 KB
testcase_16 AC 28 ms
24,336 KB
testcase_17 AC 27 ms
23,424 KB
testcase_18 AC 26 ms
23,424 KB
testcase_19 AC 25 ms
24,048 KB
testcase_20 AC 25 ms
24,264 KB
testcase_21 AC 26 ms
23,424 KB
testcase_22 AC 27 ms
23,628 KB
testcase_23 AC 26 ms
24,348 KB
testcase_24 AC 27 ms
23,856 KB
testcase_25 AC 25 ms
23,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int calc(int i, int x) {
    return x - (i - x);
}
int main () {
    int N;
    cin >> N;
    int b = N / 2;
    int a = N / 4;
    int c = a + b;
    int p, q, r;
    cout << "? " << a << endl;
    cin >> p;
    cout << "? " << b << endl;
    cin >> q;
    cout << "? " << c << endl;
    cin >> r;
    p = calc(a, p);
    q = calc(b, q);
    r = calc(c, r);
    if (p == 0) {
        cout << "! " << a + 1 << " " << N << endl;
        cin >> N;
        if (N == -1) {
            return 139;
        }
        return 0;
    }
    if (q == 0) {
        cout << "! " << b + 1 << " " << N << endl;
        cin >> N;
        if (N == -1) {
            return 139;
        }
        return 0;
    }
    if (r == 0) {
        cout << "! " << 1 << " " << c << endl;
        cin >> N;
        if (N == -1) {
            return 139;
        }
        return 0;
    }
    if (p == r) {
        cout << "! " << a + 1 << " " << c << endl;
        int x;
        cin >> x;
        if (x == -1) {
            return 139;
        }
        return 0;
    }
    int mi = 1;
    int ma = a;
    if ((p > 0) != (q > 0)) {
        mi = a;
        ma = b;
        while (mi != ma) {
            int mu = (mi + ma) / 2;
            cout << "? " << mu << endl;
            int x;
            cin >> x;
            if (x == -1) {
                return 139;
                //return 0;
            }
            x = calc(mu, x);
            if (x == 0) {
                ma = mi = mu;
                break;
            }
            if ((p > 0) == (x > 0)) {
                mi = mu + 1;
            } else {
                ma = mu;
            }
        }
        cout << "! " << mi + 1 << " " << N << endl;
        int x;
        cin >> x;
        if (x == -1) {
            return 139;
        }
        return 0;
    }
    if ((q > 0) != (r > 0)) {
        mi = b;
        ma = c;
        while (mi != ma) {
            int mu = (mi + ma) / 2;
            cout << "? " << mu << endl;
            int x;
            cin >> x;
            if (x == -1) {
                //return 139;
                return 0;
            }
            x = calc(mu, x);
            if (x == 0) {
                ma = mi = mu;
                break;
            }
            if ((q > 0) == (x > 0)) {
                mi = mu + 1;
            } else {
                ma = mu;
            }
        }
        cout << "! " << 1 << " " << mi << endl;
        int x;
        cin >> x;
        if (x == -1) {
            return 139;
        }
        return 0;
    }
    if (abs(r) > abs(p)) {
        mi = c;
        ma = N;
        swap(r, p);
    }
    while (ma != mi) {
        int mu = (ma + mi) / 2;
        cout << "? " << mu << endl;
        int x;
        cin >> x;
        if (x == -1) {
            return 139;
        }
        x = calc(mu, x);
        if (x == r) {
            ma = mi = mu;
            break;
        } else if ((x < r) == (x < p)) {
            if (mi >= c) {
                ma = mu;
            } else {
                mi = mu + 1;
            }
        } else {
            if (mi >= c) {
                mi = mu + 1;
            } else {
                ma = mu;
            }
        }
    }
    if (mi >= c) {
        cout << "! " << a + 1 << ' ' << mi << endl;
    } else {
        cout << "! " << mi + 1 << " " << c << endl;
    }
}
0