CF1823B.Sort with Step
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let's define a permutation of length n as an array p of length n, which contains every number from 1 to n exactly once.
You are given a permutation p1,p2,…,pn and a number k. You need to sort this permutation in the ascending order. In order to do it, you can repeat the following operation any number of times (possibly, zero):
- pick two elements of the permutation pi and pj such that ∣i−j∣=k, and swap them.
Unfortunately, some permutations can't be sorted with some fixed numbers k. For example, it's impossible to sort [2,4,3,1] with k=2.
That's why, before starting the sorting, you can make at most one preliminary exchange:
- choose any pair pi and pj and swap them.
Your task is to:
- check whether is it possible to sort the permutation without any preliminary exchanges,
- if it's not, check, whether is it possible to sort the permutation using exactly one preliminary exchange.
For example, if k=2 and permutation is [2,4,3,1], then you can make a preliminary exchange of p1 and p4, which will produce permutation [1,4,3,2], which is possible to sort with given k.
我们定义一个长度为 n 的排列为一个长度为 n 的数组 p,其中恰好包含从 1 到 n 的每个整数各一次。
给定一个排列 p1,p2,…,pn 和一个数 k。你需要将该排列按升序排序。为此,你可以重复执行以下操作任意次(包括零次):
- 选取排列中的两个元素 pi 和 pj,满足 ∣i−j∣=k,并交换它们。
不幸的是,对某些固定的 k,部分排列无法被排序。例如,当 k=2 时,排列 [2,4,3,1] 无法被排序。
因此,在开始排序前,你最多可以进行一次预处理交换:
- 任选一对 pi 和 pj 并交换它们。
你的任务是:
- 判断是否可以在不进行任何预处理交换的情况下将该排列排序;
- 若不能,则判断是否可以通过恰好一次预处理交换使其可排序。
例如,若 k=2 且排列为 [2,4,3,1],则你可以预先交换 p1 和 p4,得到排列 [1,4,3,2],而该排列在给定 k 下是可以被排序的。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and k (2≤n≤2⋅105; 1≤k≤n−1) — length of the permutation, and a distance between elements that can be swapped.
The second line of each test case contains n integers p1,p2,…,pn (1≤pi≤n) — elements of the permutation p.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 k(2≤n≤2⋅105;1≤k≤n−1)—— 分别表示排列的长度,以及可交换元素之间的距离。
每个测试用例的第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n)—— 排列 p 的元素。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case print
- 0, if it is possible to sort the permutation without preliminary exchange;
- 1, if it is possible to sort the permutation with one preliminary exchange, but not possible without preliminary exchange;
- -1, if it is not possible to sort the permutation with at most one preliminary exchange.
对于每个测试用例,输出:
0,如果无需预先交换即可将该排列排序;1,如果恰好需要一次预先交换才能将该排列排序,但无法在不进行预先交换的情况下完成排序;-1,如果即使最多进行一次预先交换,也无法将该排列排序。
输入输出样例
输入#1
6 4 1 3 1 2 4 4 2 3 4 1 2 4 2 3 1 4 2 10 3 4 5 9 1 8 6 10 2 3 7 10 3 4 6 9 1 8 5 10 2 3 7 10 3 4 6 9 1 8 5 10 3 2 7
输出#1
0 0 1 0 1 -1
说明/提示
In the first test case, there is no need in preliminary exchange, as it is possible to swap (p1,p2) and then (p2,p3).
In the second test case, there is no need in preliminary exchange, as it is possible to swap (p1,p3) and then (p2,p4).
In the third test case, you need to apply preliminary exchange to (p2,p3). After that the permutation becomes [3,4,1,2] and can be sorted with k=2.
在第一个测试用例中,无需预先交换,因为可以先交换 (p1,p2),再交换 (p2,p3)。
在第二个测试用例中,无需预先交换,因为可以先交换 (p1,p3),再交换 (p2,p4)。
在第三个测试用例中,需要对 (p2,p3) 执行预先交换。此后排列变为 [3,4,1,2],并可在 k=2 的条件下完成排序。
输入解题思路,AI测评打分。不知道怎么写?