Yet another MEX problem solution codechef – The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array

Yet another MEX problem solution codechef

The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:

  • The MEX of [2,2,1][2,2,1] is 00, because 00 does not belong to the array.
  • The MEX of [3,1,0,1][3,1,0,1] is 22, because 00 and 11 belong to the array, but 22 does not.
  • The MEX of [0,3,1,2][0,3,1,2] is 44, because 0,1,20,1,2 and 33 belong to the array, but 44 does not.

Yet another MEX problem solution codechef

You are given an array AA of length NN. You create a list BB consisting of the MEX-es of all subarrays of the array AA. Formally, for all pairs (l,r)(l,r) such that 1lrN1≤l≤r≤N, you calculate MEX(Al,Al+1,,Ar)MEX(Al,Al+1,…,Ar) and append the value in the list BB. Find the KK-th smallest value in the list BB.

Note: Since the size of the input and output is large, please use fast input-output methods.

Input Format Yet another MEX problem solution codechef

  • The first line contains TT denoting the number of test cases. Then the test cases follow.
  • The first line of each test case contains two space-separated integers NN and KK.
  • The second line contains NN space-separated integers A1,A2,,ANA1,A2,…,AN denoting the given array.

Output Format Yet another MEX problem solution codechef

For each test case, output on a single line the KK-th smallest value in the list BB.

Constraints Yet another MEX problem solution codechef

  • 1T31041≤T≤3⋅104
  • 1N1051≤N≤105
  • 1KN(N+1)2 1≤K≤N⋅(N+1)2 
  • 0AiN0≤Ai≤N
  • Sum of NN over all test cases does not exceed 21062⋅106.

Subtasks Yet another MEX problem solution codechef

Subtask 1 (10 points): Yet another MEX problem solution codechef

  • 1N51031≤N≤5⋅103
  • Sum of NN over all test cases does not exceed 51045⋅104.

Subtask 2 (90 points): Original constraints

Sample Input 1  Yet another MEX problem solution codechef

3
3 4
1 0 2
3 2
2 1 3
3 6
0 1 2

Sample Output 1 Yet another MEX problem solution codechef

1
0
3

Explanation Yet another MEX problem solution codechef

Test case 11: MEX(A1)=0MEX(A1)=0MEX(A1,A2)=2MEX(A1,A2)=2MEX(A1,A2,A3)=3MEX(A1,A2,A3)=3MEX(A2)=1MEX(A2)=1MEX(A2,A3)=1MEX(A2,A3)=1MEX(A3)=0MEX(A3)=0. Hence the list B=[0,2,3,1,1,0]B=[0,2,3,1,1,0] and the 44-th smallest value in BB is 11.

Test case 22: The MEX of all subarrays of the array AA is 00. Hence the 22-nd smallest element in the list BB is 00.

The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:

  • The MEX of [2,2,1][2,2,1] is 00, because 00 does not belong to the array.
  • The MEX of [3,1,0,1][3,1,0,1] is 22, because 00 and 11 belong to the array, but 22 does not.
  • The MEX of [0,3,1,2][0,3,1,2] is 44, because 0,1,20,1,2 and 33 belong to the array, but 44 does not.

Yet another MEX problem solution codechef

You are given an array AA of length NN. You create a list BB consisting of the MEX-es of all subarrays of the array AA. Formally, for all pairs (l,r)(l,r) such that 1lrN1≤l≤r≤N, you calculate MEX(Al,Al+1,,Ar)MEX(Al,Al+1,…,Ar) and append the value in the list BB. Find the KK-th smallest value in the list BB.

Note: Since the size of the input and output is large, please use fast input-output methods.

Input Format Yet another MEX problem solution codechef

  • The first line contains TT denoting the number of test cases. Then the test cases follow.
  • The first line of each test case contains two space-separated integers NN and KK.
  • The second line contains NN space-separated integers A1,A2,,ANA1,A2,…,AN denoting the given array.

Output Format Yet another MEX problem solution codechef

For each test case, output on a single line the KK-th smallest value in the list BB.

Constraints Yet another MEX problem solution codechef

  • 1T31041≤T≤3⋅104
  • 1N1051≤N≤105
  • 1KN(N+1)2 1≤K≤N⋅(N+1)2 
  • 0AiN0≤Ai≤N
  • Sum of NN over all test cases does not exceed 21062⋅106.

Subtasks Yet another MEX problem solution codechef

Subtask 1 (10 points): Yet another MEX problem solution codechef

  • 1N51031≤N≤5⋅103
  • Sum of NN over all test cases does not exceed 51045⋅104.

Subtask 2 (90 points): Original constraints

Sample Input 1  Yet another MEX problem solution codechef

3
3 4
1 0 2
3 2
2 1 3
3 6
0 1 2

Sample Output 1 Yet another MEX problem solution codechef

1
0
3

Explanation Yet another MEX problem solution codechef

Test case 11: MEX(A1)=0MEX(A1)=0MEX(A1,A2)=2MEX(A1,A2)=2MEX(A1,A2,A3)=3MEX(A1,A2,A3)=3MEX(A2)=1MEX(A2)=1MEX(A2,A3)=1MEX(A2,A3)=1MEX(A3)=0MEX(A3)=0. Hence the list B=[0,2,3,1,1,0]B=[0,2,3,1,1,0] and the 44-th smallest value in BB is 11.

Test case 22: The MEX of all subarrays of the array AA is 00. Hence the 22-nd smallest element in the list BB is 00.

Finally, progress reached the Madoka family and she decided to play with her little sister in the sensational game Space Arrays.

Yet another MEX problem solution codechef

The rules of the game are as follows:

  • Initially, a sequence a1,a2,,aNa1,a2,…,aN is given.
  • The players alternate turns.
  • In each turn, the current player must choose an index ii and increment aiai, i.e. change aiai to ai+1ai+1.
  • Afterwards, if there is no permutation p1,p2,,pNp1,p2,…,pN of the integers 11 through NN such that aipiai≤pi holds for each valid ii, the current player loses.
  • Otherwise, the game continues with the next turn.

Madoka is asking you to help her ― tell her if the first player (the player that plays in the first turn) or second player wins this game if both play optimally.

Input Yet another MEX problem solution codechef

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • The first line of each test case contains a single integer NN.
  • The second line contains NN space-separated integers a1,a2,,aNa1,a2,…,aN.

Output Yet another MEX problem solution codechef

For each test case, print a single line containing the string "First" if the first player wins or "Second" if the second player wins (without quotes).

Constraints Yet another MEX problem solution codechef

  • 1T21041≤T≤2⋅104
  • 1N21051≤N≤2⋅105
  • The sum of NN over all test cases doesn’t exceed 21052⋅105
  • 1aiN1≤ai≤N for each valid ii

Subtasks Yet another MEX problem solution codechef

Subtask #1 (100 points): Original constraints

Sample Input 1  Yet another MEX problem solution codechef

4
4
1 2 3 3
4
1 1 3 3
5
1 2 2 1 5
3
2 2 3

Sample Output 1  Yet another MEX problem solution codechef

First
Second
Second
Second

Explanation Yet another MEX problem solution codechef

Example case 1:

  • If the first player increases the fourth element, the resulting sequence is (1,2,3,4)(1,2,3,4). The second player loses after increasing any of the elements.Yet another MEX problem solution codechef
  • If the first player increases the second element, the resulting sequence is (1,3,3,3)(1,3,3,3), and he loses because there is no valid permutation. For example if p=(2,1,4,3)p=(2,1,4,3), the second element of aa is greater than the second element of pp.

Leave a Reply

Your email address will not be published. Required fields are marked *

*