CF1823B.Sort with Step

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Let's define a permutation of length nn as an array pp of length nn , which contains every number from 11 to nn exactly once.

You are given a permutation p1,p2,,pnp_1, p_2, \dots, p_n and a number kk . 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 pip_i and pjp_j such that ij=k|i - j| = k , and swap them.

Unfortunately, some permutations can't be sorted with some fixed numbers kk . For example, it's impossible to sort [2,4,3,1][2, 4, 3, 1] with k=2k = 2 .

That's why, before starting the sorting, you can make at most one preliminary exchange:

  • choose any pair pip_i and pjp_j and swap them.

Your task is to:

  1. check whether is it possible to sort the permutation without any preliminary exchanges,
  2. if it's not, check, whether is it possible to sort the permutation using exactly one preliminary exchange.

For example, if k=2k = 2 and permutation is [2,4,3,1][2, 4, 3, 1] , then you can make a preliminary exchange of p1p_1 and p4p_4 , which will produce permutation [1,4,3,2][1, 4, 3, 2] , which is possible to sort with given kk .

输入格式

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

The first line of each test case contains two integers nn and kk ( 2n21052 \le n \le 2 \cdot 10^5 ; 1kn11 \le k \le n - 1 ) — length of the permutation, and a distance between elements that can be swapped.

The second line of each test case contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n ( 1pin1 \le p_i \le n ) — elements of the permutation pp .

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10 ^ 5 .

输出格式

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.

输入输出样例

  • 输入#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)(p_1, p_2) and then (p2,p3)(p_2, p_3) .

In the second test case, there is no need in preliminary exchange, as it is possible to swap (p1,p3)(p_1, p_3) and then (p2,p4)(p_2, p_4) .

In the third test case, you need to apply preliminary exchange to (p2,p3)(p_2, p_3) . After that the permutation becomes [3,4,1,2][3, 4, 1, 2] and can be sorted with k=2k = 2 .

首页