codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1698D.md (5137B)


      1 Source: https://codeforces.com/problemset/problem/1698/D
      2 
      3 D. Fixed Point Guessing
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 This is an interactive problem.
     22 
     23 Initially, there is an array $a = [1, 2, \ldots, n]$, where $n$ is an odd positive integer. The jury has selected $\frac{n-1}{2}$ disjoint pairs of elements, and then the elements in those pairs are swapped. For example, if $a=[1,2,3,4,5]$, and the pairs $1 \leftrightarrow 4$ and $3 \leftrightarrow 5$ are swapped, then the resulting array is $[4, 2, 5, 1, 3]$.
     24 
     25 As a result of these swaps, exactly one element will not change position. You need to find this element.
     26 
     27 To do this, you can ask several queries. In each query, you can pick two integers $l$ and $r$ ($1 \leq l \leq r \leq n$). In return, you will be given the elements of the subarray $[a_l, a_{l + 1}, \dots, a_r]$ sorted in increasing order.
     28 
     29 Find the element which did not change position. You can make at most $\mathbf{15}$ queries.
     30 
     31 The array $a$ is fixed before the interaction and does not change after your queries.
     32 
     33 Recall that an array $b$ is a subarray of the array $a$ if $b$ can be obtained from $a$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
     34 
     35 Input
     36 
     37 Each test contains multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 500$) — the number of test cases. The description of the test cases follows.
     38 
     39 The first line of each test case contains an integer $n$ ($3 \leq n \lt 10^4$; $n$ is odd) — the length of the array $a$.
     40 
     41 After reading the first line of each test case, you should begin the interaction.
     42 
     43 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.
     44 
     45 Interaction
     46 
     47 For each test case, begin the interaction by reading the integer $n$.
     48 
     49 To make a query, output "$\texttt{?}\;l\;r$" ($1 \leq l \leq r \leq n$) without quotes. Afterwards, you should read in $r-l+1$ integers — the integers $a_l, a_{l + 1}, \dots, a_r$, in increasing order. You can make at most $15$ such queries in a single test case.
     50 
     51 If you receive the integer $-1$ instead of an answer or the integer $n$, it means your program has made an invalid query, has exceed the limit of queries, or has given incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
     52 
     53 When you are ready to give the final answer, output "$\texttt{!}\;x$" ($1 \leq x \leq n$) without quotes — the element that did not change position. Giving this answer does not count towards the limit of $15$ queries. Afterwards, your program must continue to solve the remaining test cases, or exit if all test cases have been solved.
     54 
     55 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:
     56 
     57 * fflush(stdout) or cout.flush() in C++;
     58 * System.out.flush() in Java;
     59 * flush(output) in Pascal;
     60 * stdout.flush() in Python;
     61 * see documentation for other languages.
     62 
     63 Hacks
     64 
     65 To make a hack, use the following format. The first line must contain an integer $t$ ($1 \leq t \leq 500$) — the number of test cases. The description of the test cases follows.
     66 
     67 The first line of each test case must contain an integer $n$ ($3 \leq n \lt 10^4$; $n$ is odd) — the length of the array $a$.
     68 
     69 The second line of each test case must contain $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements of $a$. The array $a$ should be the result of $\frac{n-1}{2}$ disjoint swaps on the array $[1,2,\dots,n]$.
     70 
     71 Example
     72 
     73 Input
     74 
     75 ```
     76 2
     77 5
     78 
     79 1 2 4 5
     80 
     81 1 3 5
     82 
     83 3
     84 
     85 1
     86 ```
     87 
     88 Output
     89 
     90 ```
     91 ? 1 4
     92 
     93 ? 3 5
     94 
     95 ! 2
     96 
     97 ? 1 1
     98 
     99 ! 1
    100 ```
    101 
    102 Note
    103 
    104 In the first test, the interaction proceeds as follows.
    105 
    106 |  |  |  |
    107 | --- | --- | --- |
    108 | Solution | Jury | Explanation |
    109 |  | $\texttt{2}$ | There are 2 test cases. |
    110 |  | $\texttt{5}$ | In the first test case, the hidden array is $[4,2,5,1,3]$, with length $5$. |
    111 | $\texttt{? 1 4}$ | $\texttt{1 2 4 5}$ | The solution requests the subarray $[4,2,5,1]$ in increasing order, and the jury responds with $[1,2,4,5]$. |
    112 | $\texttt{? 3 5}$ | $\texttt{1 3 5}$ | The solution requests the subarray $[5,1,3]$ in increasing order, and the jury responds with $[1,3,5]$. |
    113 | $\texttt{! 2}$ |  | The solution has somehow determined that $a_2=2$, and outputs it. Since the output is correct, the jury continues to the next test case. |
    114 |  | $\texttt{3}$ | In the second test case, the hidden array is $[1,3,2]$, with length $3$. |
    115 | $\texttt{? 1 1}$ | $\texttt{1}$ | The solution requests the number $[1]$ only, and the jury responds with $[1]$. |
    116 | $\texttt{! 1}$ |  | The solution has determined that $a_1=1$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. |
    117 
    118 Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.