#P16408. [Algo Beat Contest 004 D] Displaced Permutation

[Algo Beat Contest 004 D] Displaced Permutation

Background

Yet Another Interactive Problem About Permutation.

Problem Description

This is an interactive problem.

The judge system has a hidden permutation pp of 1∼n1 \sim n.

Each time, you may query two integers l,rl, r such that 1≤l≤r≤n1 \le l \le r \le n. The judge system will do the following:

  1. Cyclically shift the subsegment p[l,…,r]p[l, \ldots, r] by one.
  2. The judge system will return a 01\texttt{01} sequence ss of length nn, where si=1s_i = 1 if and only if currently pi=ip_i = i.

In this problem, cyclically shifting a non-empty sequence by one means: insert the first element of the sequence at the end, and delete the first element.

Your goal is to make the final permutation pp sorted in ascending order through a series of operations, i.e., for all i∈[1,n]i \in [1, n], we have pi=ip_i = i.

Interaction

Your program needs to interact with the judge system via standard input and output.

First, your program should read an integer nn, which is the length of the hidden permutation. Then, your program should start making queries. Each query has the format:

? l r

where l,rl, r are positive integers satisfying 1≤l≤r≤n1 \le l \le r \le n. After each query, the judge system will return a 01\texttt{01} string of length nn, and your program should read it from standard input.

When you think that pp is already sorted in ascending order, you should report it in the format:

!

After outputting the answer, your program should terminate immediately.

You can make at most 20262026 queries. If the number of queries exceeds the limit, or the answer is wrong, or the format does not meet the requirements, the judge system will return Wrong Answer.

Note: After each output, you must flush the buffer. For example, in C++ use cout << endl or fflush(stdout), and in Python use print(..., flush=True).

You may ignore the runtime of the interactive library.

4

1001

1111

? 1 4

? 2 3

!

Hint

Sample Explanation #1

In the sample, the hidden permutation is p=[4,1,3,2]p = [4, 1, 3, 2]. Explanation:

  • Query l=1,r=4l = 1, r = 4. Then pp becomes [1,3,2,4][1, 3, 2, 4], and the judge returns 1001\texttt{1001}.
  • Query l=2,r=3l = 2, r = 3. Then pp becomes [1,2,3,4][1, 2, 3, 4], and the judge returns 1111\texttt{1111}.
  • Now you think that pp has been sorted in ascending order, so you report it.

Constraints

  • 1≤n≤10001 \le n \le 1000.
  • The hidden pp in the judge system is a permutation of 1∼n1 \sim n.

Translated by ChatGPT 5