CF1558F.Strange Sort

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You have a permutation: an array a=[a1,a2,…,an]a = [a_1, a_2, \ldots, a_n] of distinct integers from 11 to nn. The length of the permutation nn is odd.

Consider the following algorithm of sorting the permutation in increasing order.

A helper procedure of the algorithm, f(i)f(i), takes a single argument ii (1≤i≤n−11 \le i \le n-1) and does the following. If ai>ai+1a_i \gt a_{i+1}, the values of aia_i and ai+1a_{i+1} are exchanged. Otherwise, the permutation doesn't change.

The algorithm consists of iterations, numbered with consecutive integers starting with 11. On the ii-th iteration, the algorithm does the following:

  • if ii is odd, call f(1),f(3),…,f(n−2)f(1), f(3), \ldots, f(n - 2);
  • if ii is even, call f(2),f(4),…,f(n−1)f(2), f(4), \ldots, f(n - 1).

It can be proven that after a finite number of iterations the permutation will be sorted in increasing order.

After how many iterations will this happen for the first time?

你有一个排列:一个由 11 到 nn 的互异整数组成的数组 a=[a1,a2,…,an]a = [a_1, a_2, \ldots, a_n]。该排列的长度 nn 为奇数。

考虑以下将该排列按升序排序的算法。

该算法中一个辅助过程 f(i)f(i) 接收单个参数 ii(其中 1≤i≤n−11 \le i \le n-1),并执行如下操作:若 ai>ai+1a_i \gt a_{i+1},则交换 aia_i 与 ai+1a_{i+1} 的值;否则,排列保持不变。

该算法由若干轮迭代组成,迭代编号从 11 开始依次递增。在第 ii 轮迭代中,算法执行如下操作:

  • 若 ii 为奇数,则依次调用 f(1),f(3),…,f(n−2)f(1), f(3), \ldots, f(n - 2);
  • 若 ii 为偶数,则依次调用 f(2),f(4),…,f(n−1)f(2), f(4), \ldots, f(n - 1)。

可以证明,经过有限轮迭代后,该排列必将变为升序排列。

问:该排列首次变为升序排列是在第几轮迭代?

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). Description of the test cases follows.

The first line of each test case contains a single integer nn (3≤n≤2⋅105−13 \le n \le 2 \cdot 10^5 - 1; nn is odd) — the length of the permutation.

The second line contains nn distinct integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n) — the permutation itself.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅105−12 \cdot 10^5 - 1.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅105−13 \le n \le 2 \cdot 10^5 - 1;且 nn 为奇数)—— 表示排列的长度。

第二行包含 nn 个互不相同的整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n)—— 即该排列本身。

保证所有测试用例中 nn 的总和不超过 2⋅105−12 \cdot 10^5 - 1。

输出格式

For each test case print the number of iterations after which the permutation will become sorted in increasing order for the first time.

If the given permutation is already sorted, print 00.

对于每个测试用例,输出排列首次变为升序排列所需的迭代次数。

如果给定的排列已经有序,则输出 00。

输入输出样例

  • 输入#1

    3
    3
    3 2 1
    7
    4 5 7 1 3 2 6
    5
    1 2 3 4 5

    输出#1

    3
    5
    0

说明/提示

In the first test case, the permutation will be changing as follows:

  • after the 11-st iteration: [2,3,1][2, 3, 1];
  • after the 22-nd iteration: [2,1,3][2, 1, 3];
  • after the 33-rd iteration: [1,2,3][1, 2, 3].

In the second test case, the permutation will be changing as follows:

  • after the 11-st iteration: [4,5,1,7,2,3,6][4, 5, 1, 7, 2, 3, 6];
  • after the 22-nd iteration: [4,1,5,2,7,3,6][4, 1, 5, 2, 7, 3, 6];
  • after the 33-rd iteration: [1,4,2,5,3,7,6][1, 4, 2, 5, 3, 7, 6];
  • after the 44-th iteration: [1,2,4,3,5,6,7][1, 2, 4, 3, 5, 6, 7];
  • after the 55-th iteration: [1,2,3,4,5,6,7][1, 2, 3, 4, 5, 6, 7].

In the third test case, the permutation is already sorted and the answer is 00.

在第一个测试用例中,排列的变化过程如下:

  • 第 11 次迭代后:[2,3,1][2, 3, 1];
  • 第 22 次迭代后:[2,1,3][2, 1, 3];
  • 第 33 次迭代后:[1,2,3][1, 2, 3]。

在第二个测试用例中,排列的变化过程如下:

  • 第 11 次迭代后:[4,5,1,7,2,3,6][4, 5, 1, 7, 2, 3, 6];
  • 第 22 次迭代后:[4,1,5,2,7,3,6][4, 1, 5, 2, 7, 3, 6];
  • 第 33 次迭代后:[1,4,2,5,3,7,6][1, 4, 2, 5, 3, 7, 6];
  • 第 44 次迭代后:[1,2,4,3,5,6,7][1, 2, 4, 3, 5, 6, 7];
  • 第 55 次迭代后:[1,2,3,4,5,6,7][1, 2, 3, 4, 5, 6, 7]。

在第三个测试用例中,排列已经有序,答案为 00。

输入解题思路,AI测评打分。不知道怎么写?

首页