Codeforces Round #700 (Div. 2) C(三分)

时间:2021-02-09 12:13:38   收藏:0   阅读:0

二分或三分。

三分:若 \([L,R]\) 有局部最小,取 m1=L+(R-L)/3m2=R-(R-L)/3? 。若 \(a_{m_1}<a_{m_2}\)\([L,m_2-1]\) 必有局部最小,否则 \([m_1+1, R]\) 必有局部最小。

总查询次数至多 \(2\lceil \log_3n\rceil\le22\)

/**
 * Codeforces Round #700 (Div. 2)
 */

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

typedef long long LL;

const int N = 1e5+5;
int a[N];
int get(int p)
{
    if (a[p]) return a[p];
    printf("? %d\n", p);
    fflush(stdout);
    scanf("%d", a+p);
    return a[p];
}
void solve()
{
    int n;
    scanf("%d", &n);
    a[0] = a[n+1] = INT_MAX;
    int i = 1, j = n;
    while (i < j) {
        int m1 = i+(j-i)/3;
        int m2 = j-(j-i)/3;
        if (get(m1) < get(m2)) j = m2-1;
        else i = m1+1;
    }
    printf("! %d\n", i);
    fflush(stdout);
}

int main()
{
    // ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    // scanf("%d", &t);
    while (t--) {
        solve();
    }
    return 0;
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!