Alphen has an array of positive integers $$$a$$$ of length $$$n$$$.
Alphen can perform the following operation:
Alphen will perform the above operation until $$$a$$$ is sorted, that is $$$a$$$ satisfies $$$a_1 \leq a_2 \leq \ldots \leq a_n$$$. How many operations will Alphen perform? Under the constraints of the problem, it can be proven that Alphen will perform a finite number of operations.
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 50$$$) — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10 ^ 9$$$) — the elements of the array $$$a$$$.
For each test case, output a single integer — the number of operations that Alphen will perform.
731 2 352 1 2 1 243 1 5 427 754 1 3 2 552 3 1 4 531000000000 1 2
0 2 5 0 4 3 1000000000
In the first test case, we have $$$a=[1,2,3]$$$. Since $$$a$$$ is already sorted, Alphen will not need to perform any operations. So, the answer is $$$0$$$.
In the second test case, we have $$$a=[2,1,2,1,2]$$$. Since $$$a$$$ is not initially sorted, Alphen will perform one operation to make $$$a=[1,0,1,0,1]$$$. After performing one operation, $$$a$$$ is still not sorted, so Alphen will perform another operation to make $$$a=[0,0,0,0,0]$$$. Since $$$a$$$ is sorted, Alphen will not perform any other operations. Since Alphen has performed two operations in total, the answer is $$$2$$$.
You are given an array of positive integers $$$a$$$ of length $$$n$$$.
Let's call an array of positive integers $$$b$$$ of length $$$n$$$ good if:
Does a good array exist?
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10 ^ 9$$$) — the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output "YES" (without quotes) if there exists a good array, and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs","yes", "Yes", and "YES" will be recognized as positive responses.
636 1 221 143 1 2 411751 2 1 1 13618343152 819343431 1000000000
YES NO YES NO NO YES
In the first test case, a possible good array is $$$[3, 3, 3]$$$. Some examples of not good arrays are:
In the second test case, $$$[1, 1]$$$ is the only array of positive integers of length $$$2$$$ that has the sum of it's elements equal to $$$2$$$. Since $$$[1, 1]$$$ is not a good array, the answer is "NO".
You are given an array of integers $$$a$$$ of length $$$n$$$.
In one operation you:
Find the maximum possible value of $$$\max(a_1, a_2, \ldots a_n)$$$ that you can get after performing this operation at most $$$k$$$ times.
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 1000$$$, $$$1 \le k \le 10^{8}$$$) — the length of the array $$$a$$$ and the maximum number of operations that can be performed.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^{8}$$$) — the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.
For each test case output a single integer — the maximum possible maximum of the array after performing at most $$$k$$$ operations.
63 41 3 35 61 3 4 5 14 131 1 3 1795 34 3 2 2 25 66 5 4 1 52 173 5
4 7 179 5 7 6
In the first test case, one possible optimal sequence of operations is: $$$[\color{red}{1}, 3, 3] \rightarrow [2, \color{red}{3}, 3] \rightarrow [\color{red}{2}, 4, 3] \rightarrow [\color{red}{3}, 4, 3] \rightarrow [4, 4, 3]$$$.
In the second test case, one possible optimal sequence of operations is: $$$[1, \color{red}{3}, 4, 5, 1] \rightarrow [1, \color{red}{4}, 4, 5, 1] \rightarrow [1, 5, \color{red}{4}, 5, 1] \rightarrow [1, 5, \color{red}{5}, 5, 1] \rightarrow [1, \color{red}{5}, 6, 5, 1] \rightarrow [1, \color{red}{6}, 6, 5, 1] \rightarrow [1, 7, 6, 5, 1]$$$.
This is an interactive problem.
The jury has hidden a permutation$$$^\dagger$$$ $$$p$$$ of length $$$n$$$.
In one query, you can pick two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \lt r \le n$$$) by paying $$$(r - l)^2$$$ coins. In return, you will be given the number of inversions$$$^\ddagger$$$ in the subarray $$$[p_l, p_{l + 1}, \ldots p_r]$$$.
Find the index of the maximum element in $$$p$$$ by spending at most $$$5 \cdot n^2$$$ coins.
Note: the grader is not adaptive: the permutation is fixed before any queries are made.
$$$^\dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
$$$^\ddagger$$$ The number of inversions in an array is the number of pairs of indices $$$(i,j)$$$ such that $$$i \lt j$$$ and $$$a_i \gt a_j$$$. For example, the array $$$[10,2,6,3]$$$ contains $$$4$$$ inversions. The inversions are $$$(1,2),(1,3),(1,4)$$$, and $$$(3,4)$$$.
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.
The only line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$) — the length of the hidden permutation $$$p$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.
The interaction for each test case begins by reading the integer $$$n$$$.
To make a query, output "? l r" ($$$1 \le l \lt r \le n$$$) without quotes. Afterwards, you should read one single integer — the answer for your query.
If you receive the integer $$$-1$$$ instead of an answer or a valid value of $$$n$$$, it means your program has made an invalid query, has exceed the limit of queries, or has given an 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.
When you are ready to give the final answer, output "! i" ($$$1 \le i \le n$$$) without quotes — the index of the maximum of the hidden permutation. After solving a test case, your program should move to the next one immediately. After solving all test cases, your program should be terminated immediately.
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:
Hacks
To hack, use the following format:
The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$) — the length of the hidden permutation $$$p$$$.
The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots, p_n$$$ ($$$1 \le p_i \le n$$$), $$$p$$$ must be a permutation.
The sum of $$$n$$$ over all test cases should not exceed $$$2000$$$.
2 4 1 0 2 1
? 1 3 ? 3 4 ! 4 ? 1 2 ! 1
In the first test, the interaction proceeds as follows:
| Solution | Jury | Explanation |
| 2 | There are $$$2$$$ test cases. | |
| 4 | In the first test case, the hidden permutation is $$$[1,3,2,4]$$$, with length $$$4$$$. | |
| ? 1 3 | 1 | The solution requests the number of inversions in the subarray $$$[1,3,2]$$$ by paying $$$4$$$ coins, and the jury responds with $$$1$$$. |
| ? 3 4 | 0 | The solution requests the number of inversions in the subarray $$$[2,4]$$$ by paying $$$1$$$ coin, and the jury responds with $$$0$$$. |
| ! 4 | The solution has somehow determined that $$$p_4 = 4$$$, and outputs it. Since the output is correct, the jury continues to the next test case. | |
| 2 | In the second test case, the hidden permutation is $$$[2,1]$$$, with length $$$2$$$. | |
| ? 1 2 | 1 | The solution requests the number of inversions in the subarray $$$[2,1]$$$ by paying $$$1$$$ coin, and the jury responds with $$$1$$$. |
| ! 1 | The solution has somehow determined that $$$p_1 = 2$$$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. |
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.
This is the easy version of the problem. The differences between the two versions are the constraint on $$$n$$$ and the time limit. You can make hacks only if both versions of the problem are solved.
You are given a tree with $$$n$$$ vertices rooted at vertex $$$1$$$.
For some permutation$$$^\dagger$$$ $$$a$$$ of length $$$n$$$, let $$$f(a)$$$ be the number of pairs of vertices $$$(u, v)$$$ such that $$$a_u \lt a_{\operatorname{lca}(u, v)} \lt a_v$$$. Here, $$$\operatorname{lca}(u,v)$$$ denotes the lowest common ancestor of vertices $$$u$$$ and $$$v$$$.
Find the maximum possible value of $$$f(a)$$$ over all permutations $$$a$$$ of length $$$n$$$.
$$$^\dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5000$$$).
The second line contains $$$n - 1$$$ integers $$$p_2,p_3,\ldots,p_n$$$ ($$$1 \le p_i \lt i$$$) indicating that there is an edge between vertices $$$i$$$ and $$$p_i$$$.
Output the maximum value of $$$f(a)$$$.
5 1 1 3 3
4
2 1
0
6 1 2 2 1 5
7
4 1 1 1
2
The tree in the first test:
One possible optimal permutation $$$a$$$ is $$$[2, 1, 4, 5, 3]$$$ with $$$4$$$ suitable pairs of vertices:
The tree in the third test:
The tree in the fourth test:
This is the hard version of the problem. The differences between the two versions are the constraint on $$$n$$$ and the time limit. You can make hacks only if both versions of the problem are solved.
You are given a tree with $$$n$$$ vertices rooted at vertex $$$1$$$.
For some permutation$$$^\dagger$$$ $$$a$$$ of length $$$n$$$, let $$$f(a)$$$ be the number of pairs of vertices $$$(u, v)$$$ such that $$$a_u \lt a_{\operatorname{lca}(u, v)} \lt a_v$$$. Here, $$$\operatorname{lca}(u,v)$$$ denotes the lowest common ancestor of vertices $$$u$$$ and $$$v$$$.
Find the maximum possible value of $$$f(a)$$$ over all permutations $$$a$$$ of length $$$n$$$.
$$$^\dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$).
The second line contains $$$n - 1$$$ integers $$$p_2,p_3,\ldots,p_n$$$ ($$$1 \le p_i \lt i$$$) indicating that there is an edge between vertices $$$i$$$ and $$$p_i$$$.
Output the maximum value of $$$f(a)$$$.
5 1 1 3 3
4
2 1
0
6 1 2 2 1 5
7
4 1 1 1
2
The tree in the first test:
One possible optimal permutation $$$a$$$ is $$$[2, 1, 4, 5, 3]$$$ with $$$4$$$ suitable pairs of vertices:
The tree in the third test:
The tree in the fourth test: