| Time Limit: 1 second | |
| Memory Limit: 256 MB | |
| Overview | |
| This is an INTERACTIVE PROBLEM. There are N kinds of minerals. For each kind there are exactly 2 slices, for a total of 2N slices numbered 1..2N. The judge fixes a hidden pairing: for each kind, the two slices of that kind form one pair. Your task is to determine all N pairs. | |
| You have access to a device. You may insert or extract slices from the device one at a time. After each operation, you learn how many DISTINCT kinds of minerals are currently present among the slices inside the device. | |
| Goal | |
| Determine all N pairs while MINIMIZING the number of queries you use. You may use at most 1,000,000 queries. Any correct solution using β€ 1,000,000 queries is accepted; fewer queries are considered better. | |
| Interaction Protocol (standard input/output) | |
| 1) At the start, the judge outputs a single integer: | |
| N | |
| β’ 1 β€ N β€ 43,000. | |
| 2) You may then repeatedly perform queries. To toggle the presence of slice x in the device: | |
| β’ Output a line: ? x | |
| where 1 β€ x β€ 2N | |
| β’ Flush stdout. | |
| β’ Read a single integer r from stdin. | |
| r is the number of DISTINCT kinds currently present among all slices inside the device after this toggle: | |
| β If x was not in the device, it is now inserted. | |
| β If x was already in the device, it is extracted. | |
| β r counts how many mineral kinds appear at least once among the slices currently in the device. | |
| 3) When you have determined a pair (a, b), output exactly one line: | |
| β’ Output: ! a b | |
| where 1 β€ a β€ 2N and 1 β€ b β€ 2N. | |
| Over the entire run you must output exactly N such lines, and together they must use each index 1..2N exactly once. | |
| 4) Order is flexible: | |
| β’ You may interleave β? xβ queries and β! a bβ answers in any order. | |
| β’ The judge terminates the interaction immediately after reading the N-th valid β! a bβ line. Do not send any further output after that point. | |
| Important Rules and Constraints | |
| β’ Only print lines of the form β? xβ and β! a bβ. | |
| β’ Indices in queries and answers must satisfy their ranges. | |
| β’ Exactly N answer lines must be printed and together cover each index 1..2N exactly once. | |
| β’ A βqueryβ is defined as one printed line β? xβ. You may perform at most 1,000,000 queries. | |
| β’ Flush stdout after every line you print (interactive). | |
| β’ If you violate the protocol (bad format, invalid index, wrong pairings, too many queries, wrong number of answers), the judge will return a Wrong Answer verdict with a message. | |
| Device Behavior (for clarity) | |
| β’ The device maintains a set S of slices currently inside. | |
| β’ Query β? xβ toggles membership of x in S: | |
| β If x β S, insert x. | |
| β Else (x β S), remove x. | |
| β’ The judge replies with r = number of DISTINCT mineral kinds represented by S. If S is empty, r = 0. | |
| Scoring / Ratio (informative) | |
| β’ Let Q be your total number of β? xβ queries. | |
| β’ The judge also knows an optimal_queries value for the instance. | |
| β’ Your ratio is (1,000,000 β Q) / (1,000,000 β optimal_queries). | |
| β’ The judge reports this ratio and scores only on Accepted submissions. | |
| Sample Communication | |
| Judge β program: | |
| 4 | |
| Program β judge: | |
| ? 1 | |
| Judge β program: | |
| 1 | |
| Program β judge: | |
| ? 2 | |
| Judge β program: | |
| 2 | |
| Program β judge: | |
| ? 5 | |
| Judge β program: | |
| 2 | |
| Program β judge: | |
| ? 2 | |
| Judge β program: | |
| 1 | |
| Program β judge: | |
| ! 3 4 | |
| Program β judge: | |
| ! 5 1 | |
| Program β judge: | |
| ! 8 7 | |
| Program β judge: | |
| ! 2 6 | |
| (Here, the program used 4 queries total.) | |