#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 of .
Each time, you may query two integers such that . The judge system will do the following:
- Cyclically shift the subsegment by one.
- The judge system will return a sequence of length , where if and only if currently .
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 sorted in ascending order through a series of operations, i.e., for all , we have .
Interaction
Your program needs to interact with the judge system via standard input and output.
First, your program should read an integer , which is the length of the hidden permutation. Then, your program should start making queries. Each query has the format:
? l r
where are positive integers satisfying . After each query, the judge system will return a string of length , and your program should read it from standard input.
When you think that 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 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 . Explanation:
- Query . Then becomes , and the judge returns .
- Query . Then becomes , and the judge returns .
- Now you think that has been sorted in ascending order, so you report it.
Constraints
- .
- The hidden in the judge system is a permutation of .
Translated by ChatGPT 5