B. Sort with Step

B. Sort with Step
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
.

Input
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
.

Output
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.
Example
inputCopy
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
outputCopy
0
0
1
0
1
-1
Note
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
做的时候想了好久都没想出来,没想到这么简单

#include<bits/stdc++.h>
using namespace std;
int k,n;
int a[200020];
int main() {
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> k;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			a[i]--;
		}
		int res = 0;
		for (int i = 0; i < n; i++)
			if (a[i] % k != i % k)
				res++;
		if (res == 0)cout << 0 << endl;
		else if (res == 2)cout << 1 << endl;
		else cout << -1 << endl;
	}
}

.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值