Monotone Subsequence

시간 제한2초메모리 제한2048 MB

요약
길이 n^2+1인 숨겨진 순열에서 증가하거나 감소하는 길이 n+1 부분수열을 찾는다. 선택한 인덱스 집합의 왼쪽부터 보이는 최댓값들을 돌려주는 질의를 최대 n번 쓸 수 있다.
난이도

어려움10점 중 8점

유형
이분 탐색, 그리디, 구간, 구현
정답자
아직 제출이 없습니다

문제

This is an interactive problem.

Faker is being naughty again. You asked him to create a nice query problem, but he created an interactive problem where he is answering a query instead! Faker hid a permutation from you, and you have to infer some interesting information by interacting with him.

You are given an integer nn. Faker hid a hidden permutation* p_1,p_2,…,p_n2+1p\_1, p\_2, \ldots, p\_{n^2+1} of length n2+1n^2+1. Your goal is to find a monotone subsequence (either increasing or decreasing) of the hidden permutation, with length exactly n+1n+1. It can be proved that every permutation of length n2+1n^2 + 1 contains a monotone subsequence of length n+1n+1. For more information about the proof, you can check out this Wikipedia page.

To find it, you can make at most nn skyscraper queries to the interactor, which is defined as follows:

  • You provide a set of kk indices as a strictly increasing sequence: i_1,i_2,…,i_ki\_1, i\_2, \ldots, i\_k.
  • The interactor considers the values of the hidden permutation at these indices: p_i_1,p_i_2,…,p_i_kp\_{i\_1}, p\_{i\_2}, \ldots, p\_{i\_k}.
  • The interactor then returns the indices corresponding to the visible skyscrapers from this set. An index i_ji\_j is visible if its value p_i_jp\_{i\_j} is greater than the values of all preceding elements in your query, i.e., p_i_j>p_i_mp\_{i\_j} > p\_{i\_m} for all 1≤m<j1 \le m < j. This is equivalent to finding the indices of the left-to-right maxima of the sequence (p_i_1,…,p_i_k)(p\_{i\_1}, \ldots, p\_{i\_k}).

After making at most nn queries, you must report a valid monotone subsequence of length exactly n+1n+1.

Note that the permutation pp is fixed before any queries are made and does not depend on the queries.


*A permutation of length mm is an array consisting of mm distinct integers from 11 to mm in arbitrary order. For example, \[2,3,1,5,4]\[2,3,1,5,4] is a permutation, but \[1,2,2]\[1,2,2] is not a permutation (22 appears twice in the array), and \[1,3,4]\[1,3,4] is also not a permutation (m=3m=3 but there is 44 in the array).

입력

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤50001 \le t \le 5000). The description of the test cases follows.

The first and only line of each test case contains a single integer nn (1≤n≤1001 \le n \le 100).

It is guaranteed that the sum of n2+1n^2+1 over all test cases does not exceed 10,00110\\,001.

힌트

For the first test case, n=1n=1. The hidden permutation is p=\[1,2]p=\[1, 2].

  • For the query ? 2 1 2, the visible skyscrapers are at indices 11 and 22. The interactor returns 2 1 2.
  • An increasing subsequence of length 22 at indices 1,21, 2 is reported.

For the second test case, n=2n=2. The hidden permutation is p=\[5,3,4,1,2]p=\[5, 3, 4, 1, 2].

  • For the query ? 3 1 2 3, the visible skyscraper is at index 11. The interactor returns 1 1.
  • For the query ? 3 2 3 5, the visible skyscrapers are at indices 22 and 33. The interactor returns 2 2 3.
  • A decreasing subsequence of length 33 at indices 1,3,41, 3, 4 is reported.

Although Faker will play the role of interactor, the interactor will never lie to you.

예제1

  1. 예제 1

    입력
    2
    1
    
    2 1 2
    
    2
    
    1 1
    
    2 2 3
    
    
    예상 출력
    
    
    ? 2 1 2
    
    ! 1 2
    
    ? 3 1 2 3
    
    ? 3 2 3 5
    
    ! 1 3 4