#P9347. 似曾相识燕归来

    ID: 9875 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>洛谷原创Special JudgeO2优化排序构造洛谷月赛分类讨论

似曾相识燕归来

Background

As the spring rain is about to end, a swallow suddenly chirps softly, stirring up ripples on the spring water. The ripples bring back memories from last year. The memories have a fragrance, filling the eaves. Under the eaves, the old swallow nest is worn and broken. Tears cannot help but fall. Looking up at the returning swallows, something moves in the heart, as if they are familiar faces from long ago.

Problem Description

nn swallows fly across the sunset. In order from front to back, the size of the ii-th swallow is pip_i, and pp is a permutation of length nn.

Now you may perform at most LL operations of the following type:

  • Choose three integers i,j,ki, j, k such that 1i<j<kn1 \le i < j < k \le n. If pi>pkp_i > p_k, swap the ii-th and jj-th swallows; otherwise, swap the jj-th and kk-th swallows.

To make the formation neat, we want the swallows to be in increasing order from front to back, that is, pi=ip_i = i for all 1in1 \le i \le n.

Determine whether the goal can be achieved. If yes, construct a sequence of operations that satisfies the requirements.

Input Format

This problem contains multiple test cases.

The first line contains an integer TT, the number of test cases.

For each test case:

  • The first line contains two integers n,Ln, L.
  • The second line contains nn integers p1,p2,,pnp_1, p_2, \ldots, p_n.

Output Format

For each test case:

  • If it is impossible to make pp increasing using at most LL operations, output one line -1.
  • Otherwise, output an integer xx on the first line, indicating the number of operations. Then output xx lines, each containing three integers i,j,ki, j, k, describing an operation. You must guarantee that 0xL0 \le x \le L and 1i<j<kn1 \le i < j < k \le n.
1
4 4
4 2 1 3
2
1 3 4
2 3 4

Hint

[Hint]

A permutation of length nn is an array in which every positive integer from 11 to nn appears exactly once. For example, [3,1,2][3,1,2] is a permutation of length 33, while [5,5,1,2,3][5,5,1,2,3] is not a permutation.

[Explanation for Sample 1]

  • In the first operation, i=1,j=3,k=4i = 1, j = 3, k = 4. Since p1>p4p_1 > p_4, we swap p1p_1 and p3p_3, and now p=[1,2,4,3]p = [1,2,4,3].
  • In the second operation, i=2,j=3,k=4i = 2, j = 3, k = 4. Since p2<p4p_2 < p_4, we swap p3p_3 and p4p_4, and now p=[1,2,3,4]p = [1,2,3,4].

[Constraints]

This problem uses bundled testdata.

  • Subtask 1 (5 points): n3n \le 3.
  • Subtask 2 (5 points): n4n \le 4.
  • Subtask 3 (5 points): T50T \le 50, n8n \le 8.
  • Subtask 4 (10 points): n8n \le 8.
  • Subtask 5 (25 points): L=n+2L = n + 2.
  • Subtask 6 (25 points): L=n+1L = n + 1.
  • Subtask 7 (25 points): no special restrictions.

For 100%100\% of the testdata, 1T1051 \le T \le 10^5, 1n,n2×1061 \le n, \sum n \le 2 \times 10^6, nLn+2n \le L \le n + 2, and pp is a permutation of 1n1 \sim n.

Translated by ChatGPT 5