C. Searching Local Minimum

CF Round #700

Posted by XDong on February 8, 2021

Searching Local Minimum

This is an interactive problem.

Homer likes arrays a lot and he wants to play a game with you.

Homer has hidden from you a permutation a1,a2,…,an of integers 1 to n. You are asked to find any index k (1≤k≤n) which is a local minimum.

For an array a1,a2,…,an, an index i (1≤i≤n) is said to be a local minimum if ai<min{ai−1,ai+1}, where a0=an+1=+∞. An array is said to be a permutation of integers 1 to n, if it contains all integers from 1 to n exactly once.

Initially, you are only given the value of n without any other information about this permutation.

At each interactive step, you are allowed to choose any i (1≤i≤n) and make a query with it. As a response, you will be given the value of ai.

You are asked to find any index k which is a local minimum after at most 100 queries.

Interaction You begin the interaction by reading an integer n (1≤n≤105) on a separate line.

To make a query on index i (1≤i≤n), you should output “? i” in a separate line. Then read the value of ai in a separate line. The number of the “?” queries is limited within 100.

When you find an index k (1≤k≤n) which is a local minimum, output “! k” in a separate line and terminate your program.

In case your query format is invalid, or you have made more than 100 “?” queries, you will receive Wrong Answer verdict.

After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack Format

The first line of the hack should contain a single integer n (1≤n≤105).

The second line should contain n distinct integers a1,a2,…,an (1≤ai≤n).

中文大意

通过小于100次的查询,求出给定排列的一个局部最小值

官方题解

tutorial

解析

初始条件下 $l = 1$, $r = n$, $a_l < a_0$, $a_r < a_n$, $mid = (l + r) / 2$

经过不断二分仍然满足条件$a_l < a_{l-1}$, $a_r < a_{r+1}$

  1. 如果$a_m < a_{m+1}$,$r = m$
  2. 如果$a_m > a_{m+1}$,$l = m+1$

当$l = r$时,得出局部最小值$l$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 1e5 + 5;
int n, arr[MAX_N];

void query(int x) {
  if (1 <= x && x <= n) {
    cout << "? " << x << endl;
    cout.flush();
    cin >> arr[x];
  }
}

int main() {
  cin >> n;
  arr[0] = arr[n + 1] = n + 1;
  // 初始情况 arr[l - 1] > arr[l]  && arr[r + 1] > arr[r]
  // 当 l = r 时两个条件同时满足即l = r为局部最小值
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    query(mid);
    query(mid + 1);
    if (arr[mid] < arr[mid + 1]) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << "! " << l << endl;
  return 0; 
}