Indivisible Inversions

시간 제한2초메모리 제한256 MB

요약
순열이 주어질 때, 역전 수가 K로 나누어떨어지지 않는 가장 긴 연속 부분 배열의 길이를 구하거나 그런 배열이 없으면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
분할 정복, 누적 합, 조합론, 수학
정답자
아직 제출이 없습니다

문제

While studying for his algorithms midterm, Busy Beaver came up with the following problem and wants your help to solve it.

You are given an integer NN and a permutation p_1,p_2,…,p_Np\_1,p\_2,\ldots,p\_N of length NN.1 Find the length of the longest contiguous subarray (l,rl,r) (1≤l≤r≤N1 \le l \le r \le N) of pp such that the number of inversions2 of p_l,p_l+1,…,p_rp\_l,p\_{l+1},\ldots,p\_r is not divisible by KK, or determine if such a subarray does not exist.

Help Busy Beaver find the answer to this problem!


1A permutation of length NN is an array consisting of NN distinct integers from 11 to NN in arbitrary order. For example, \[2,3,1,5,4]\[2,3,1,5,4] is a permutation, but \[1,2,2]\[1,2,2] is not a permutation (22 appears twice in the array), and \[1,3,4]\[1,3,4] is also not a permutation (N=3N=3 but there is 44 in the array).

2An inversion in a permutation p is a pair of indices (i,j)(i, j) such that i>ji > j and p_i<p_jp\_i < p\_j. For example, a permutation \[4,1,3,2]\[4, 1, 3, 2] contains 4 inversions: (2,1)(2, 1), (3,1)(3, 1), (4,1)(4, 1), (4,3)(4, 3).

입력

Each test contains multiple test cases. The first line of input contains a single positive integer TT, the number of test cases (1≤T≤104)(1 \leq T \leq 10^4). The description of each test case follows.

The first line of each test case contains two integers NN and KK (2≤K≤N≤2⋅1052 \le K \le N \le 2 \cdot 10^5).

The second line of each test case contains NN distinct positive integers p_1,p_2,…,p_Np\_1,p\_2,\ldots,p\_N (1≤p_i≤N1 \le p\_i \le N).

It is guaranteed that the sum of NN across all test cases does not exceed 2⋅1052 \cdot 10^5.

출력

For each test case, output one line with a single integer, indicating the length of the longest contiguous subarray of pp such that the number of inversions of the subarray is not divisible by KK. If such a subarray does not exist, output −1-1.

힌트

In the first test case, the number of inversions of \[1,3,2,4]\[1,3,2,4] is 11 (the second and third elements form the only inversion pair). Since 11 is not divisible by 33, the longest contiguous subarray whose number of inversions isn't divisible by 55 is the entire array itself. Thus, the answer to the test case is the length of the entire array, which is 44.

In the second test case, the number of inversions of each contiguous subarray is 00 because the array is sorted. Since no contiguous subarray has an inversion count not divisible by 55, the answer is −1-1.

In the third test case, it can be shown that the longest contiguous subarray whose number of inversions is not divisible by 22 is \[3,1,4,2,6]\[3, 1, 4, 2, 6], which has 33 inversions.

In the fourth test case, it can be shown that the longest contiguous subarray whose number of inversions is not divisible by 22 is \[5,1,4,6,2]\[5, 1, 4, 6, 2], which has 55 inversions.

예제1

  1. 예제 1

    입력
    4
    4 3
    1 3 2 4
    5 5
    1 2 3 4 5
    7 2
    3 1 4 2 6 5 7
    7 2
    7 5 1 4 6 2 3
    
    예상 출력
    4
    -1
    5
    5