#P94. Interactive Problem Test 3: A Tough Binary Search

Interactive Problem Test 3: A Tough Binary Search

Person in charge

Problem Description

We have a binary search implementation (whose code you cannot see as it's implemented in the judge). Your task is to write an adaptive interaction library that ensures the actual number of searches performed is never less than the theoretical minimum required to guarantee successful search.

Interaction Protocol

This is an interactive problem.

Use the following template:

#include <bits/stdc++.h>
using namespace std;
int respond(int n, int q, int x, vector<pair<int, int>> p)
{
    int z = 0;
    // ...
    return z;
}

Parameters:

  • nn: Upper bound for current binary search. The target is a positive integer 1yn1 \le y \le n.
  • qq: Number of searches already performed in this round (excluding current search).
  • xx: Current pivot value. Return zz based on yy's relation to xx:
    • z=0z=0 if y>xy > x
    • z=1z=1 if y=xy = x
    • z=2z=2 if y<xy < x
    • You may dynamically modify yy as long as it doesn't contradict previous search information.
  • pp: Array of length qq (1-indexed) containing previous search info in this round:
    • Each pair u,vu,v represents:
      • uu: xx value passed by judge
      • vv: zz value returned by your program
    • Guaranteed to be in chronological order.

Constraints:

  • 1x,un1041 \le x,u \le n \le 10^4
  • 0q350 \le q \le 35
  • v{0,2}v \in \{0,2\}
  • Function will be called at most 10610^6 times
  • Total search rounds does not exceed 4×1044 \times 10^4

Sample

Three search rounds are shown below (AC solution):

Call Return Explanation
respond(4, 0, 1, []); 00 Round 11 starts. Your program sets y=2y=2
respond(4, 1, 2, [(1, 0)]); 00 Your program changes yy to 33
respond(4, 2, 4, [(1, 0), (2, 0)]); 22 yy remains 33
respond(4, 3, 3, [(1, 0), (2, 0), (4, 2)]); 11 Round 11 ends (44 searches)
respond(1, 0, 1, []); 11 Round 22 ends immediately (11 search)
respond(5, 0, 3, []); 22 Round 33 starts. Your program sets y=2y=2
respond(5, 1, 2, [(3, 2)]); 22 Your program changes yy to 11
respond(5, 2, 1, [(3, 2), (2, 2)]); 11 Round 33 ends (33 searches)