아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Irreducible Permutation

시간 제한1초메모리 제한1024 MB

요약
주어진 순열을 기약 순열로 만들기 위한 인접 교환의 최소 횟수와 그 교환 순서를 구한다.
난이도

보통10점 중 6점

유형
그리디, 누적 합, 완전 탐색
정답자
아직 제출이 없습니다

문제

A permutation is called irreducible if none of its prefixes forms a permutation, except the permutation itself. For example, \[2,3,1]\[2,3,1] and \[4,1,2,3]\[4,1,2,3] are irreducible while \[2,1,3]\[2,1,3] and \[1,3,2]\[1,3,2] are not.

You are given a permutation PP of length NN. In one operation, you can choose any two adjacent indices and swap their values.

Find the minimum number, and the corresponding sequence, of operations to transform PP into an irreducible permutation. It can be shown that you can always make given permutation irreducible.

입력

The first line contains a single integer TT --- the number of test cases.

The first line of each test case contains a single integer NN.

The second line of each test case contains NN space-separated integers P_1,…,P_NP\_1,\ldots ,P\_N (1≤P_i≤N)(1\le P\_i\le N).

출력

For each test case, print two lines:

On the first line, print KK --- the minimum number of operations that can make PP irreducible.

On the second line, print KK space-separated integers S_1,…,S_KS\_1,\ldots ,S\_K where S_iS\_i and S_i+1S\_i+1 are the indices you intend to swap. If there are multiple solutions, you may print any.

Note that the operations are performed sequentially in the same order specified by your output.

제한

  • 1≤T≤100,0001\le T\le 100\\, 000
  • 1≤N≤100,0001\le N\le 100\\, 000
  • 1≤P_i≤N (1≤i≤N)1\le P\_i\le N\ (1\le i\le N)
  • P_i≠P_jP\_i\neq P\_j if i≠j (1≤i,j≤N)i\neq j\ (1\le i,j\le N)
  • It is guaranteed that the sum of NN over all test cases does not exceed 500,000500\\, 000.
  • 1≤S_i≤N−1 (1≤i≤K)1\le S\_i\le N-1\ (1\le i\le K)

예제1

  1. 예제 1

    입력
    3
    4
    1 2 3 4
    3
    2 3 1
    5
    3 1 2 4 5
    
    예상 출력
    3
    1 2 3
    0
    
    2
    4 3