Codeforces Round #503 (by SIS, Div. 2) D. The hat

Posted basasuya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #503 (by SIS, Div. 2) D. The hat相关的知识,希望对你有一定的参考价值。

技术分享图片

有图可以直观发现,如果一开始的pair(1,1+n/2)和pair(x, x+n/2)大小关系不同
那么中间必然存在一个答案

简单总结就是大小关系不同,中间就有答案
所以就可以使用二分

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;


int main() {
    int n;
    while(~scanf("%d", &n)) {
        printf("? 1
");
        fflush(stdout);
        int t1; scanf("%d", &t1);

        printf("? %d
", 1 + n/2);
        fflush(stdout);
        int t2; scanf("%d", &t2);

        bool flag = t1 > t2;
        if(t1 == t2) {
            printf("! 1
");
            fflush(stdout);
            continue;
        }

        if(n == 2) {
            printf("! -1
");
            fflush(stdout);
            continue;
        }

        int l = 1; int r = 1+n/2;

        bool success = false;
        while(l < r - 1) {
            int mid = (l + r) >>1;
            int mid2 = mid + n/2;

            printf("? %d
", mid);
            fflush(stdout);
            int t1; scanf("%d", &t1);

            printf("? %d
", mid2);
            fflush(stdout);
            int t2; scanf("%d", &t2);

            if(t1 == t2) {
                success = true;
                printf("! %d
", mid);
                fflush(stdout);
                break;
            }
            if( (t1 < t2) ^ flag) l = mid;
            else r = mid;
        }

        if(!success) {
            printf("! -1
");
            fflush(stdout);
        }
    }
    return 0;
}


以上是关于Codeforces Round #503 (by SIS, Div. 2) D. The hat的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #503 (by SIS, Div. 2) C. Elections

Codeforces Round #503 (by SIS, Div. 2)

Codeforces Round #503 (by SIS, Div. 2)-C. Elections

Codeforces Round #503 (by SIS, Div. 2) Partial Solution

Codeforces Round #503 (by SIS, Div. 2) C. Elections

Codeforces Round #503 (by SIS, Div. 2) D. The hat