Codeforces Round #648 (Div. 2)
A. Matrix Game
Ashish and Vivek play a game on a matrix consisting of rows and columns, where they take turns claiming cells. Unclaimed cells are represented by , while claimed cells are represented by . The initial state of the matrix is given. There can be some claimed cells in the initial state.
In each turn, a player must claim a cell. A cell may be claimed if it is unclaimed and does not share a row or column with any other already claimed cells. When a player is unable to make a move, he loses and the game ends.
If Ashish and Vivek take turns to move and Ashish goes first, determine the winner of the game if both of them are playing optimally.
Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves.
Input
The first line consists of a single integer — the number of test cases. The description of the test cases follows.
The first line of each test case consists of two space-separated integers — the number of rows and columns in the matrix.
he following lines consist of m integers each, the -th integer on the -th line denoting .
Output
For each test case if Ashish wins the game print “Ashish” otherwise print “Vivek” (without quotes).
Example
input
4
2 2
0 0
0 0
2 2
0 0
0 1
2 3
1 0 1
1 1 0
3 3
1 0 0
0 0 0
1 0 0
output
Vivek
Ashish
Vivek
Ashish
Note
For the first case: One possible scenario could be: Ashish claims cell , Vivek then claims cell . Ashish can neither claim cell , nor cell as cells and are already claimed. Thus Ashish loses. It can be shown that no matter what Ashish plays in this case, Vivek will win.
For the second case: Ashish claims cell , the only cell that can be claimed in the first move. After that Vivek has no moves left.
For the third case: Ashish cannot make a move, so Vivek wins.
For the fourth case: If Ashish claims cell (2,3), Vivek will have no moves left.
Translation
给一个元素为 和 组成的矩阵,两个人轮流选择一个 将其变成 ,可选的条件是这个 所在的行和列上都没有 。当某个人无法选择时,这个人就视为输家。
Idea
简单博弈论。
设不存在 的行数为 ,不存在 的列数为 ,则能够修改的位置的数量为 ,若 为偶数,则后手胜,否则先手胜。
Code
1 |
|
B. Trouble Sort
Ashish has elements arranged in a line.
These elements are represented by two integers — the value of the element and — the type of the element (there are only two possible types: and ). He wants to sort the elements in non-decreasing values of .
He can perform the following operation any number of times:
Select any two elements and such that and swap them. That is, he can only swap two elements of different types in one move.
Tell him if he can sort the elements in non-decreasing values of ai after performing any number of operations.
Input
The first line contains one integer — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer — the size of the arrays.
The second line contains integers — the value of the -th element.
The third line contains integers $(b_i∈\{0,1\})$ — the type of the -th element.
Output
For each test case, print “Yes” or “No” (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.
You may print each letter in any case (upper or lower).
Example
input
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1
output
Yes
Yes
Yes
No
Yes
Note
For the first case: The elements are already in sorted order.
For the second case: Ashish may first swap elements at positions and , then swap elements at positions and .
For the third case: The elements are already in sorted order.
For the fourth case: No swap operations may be performed as there is no pair of elements and such that . The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions and , then elements at positions and .
Translation
给一个数组,数组中第 个元素的值为 ,第 个元素的属性为 $b_i\in\{0,1\}$。
可以选择两个属性不同的元素进行交换,问能在进行若干次操作后,讲数组中元素的值排成非递减的序列。
Idea
①当 本身非递减时,一定能满足要求。
②如果需要进行交换才能满足要求,结论是:只要数组中两种属性都存在,那么在经过若干次元素交换后,元素的值一定能形成非递减序列。下面给出证明:设想我们对数组中元素的值进行冒泡排序,假如要交换 和 ,如果两者的属性不同,那么直接交换;如果两者属性相同,可以借助一个属性不同的元素,进行三次交换 a b x,a x b,x a b,b a x
使得 和 交换, 处在原位,按照冒泡排序的原理,一定能排成非递减的序列。
Code
1 |
|
C. Rotation Matching
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size . Let’s call them and .
Note that a permutation of elements is a sequence of numbers , in which every number from to appears exactly once.
The message can be decoded by an arrangement of sequence and , such that the number of matching pairs of elements between them is maximum. A pair of elements and is said to match if:
- , that is, they are at the same index.
His two disciples are allowed to perform the following operation any number of times: choose a number and cyclically shift one of the permutations to the left or right times.
A single cyclic shift to the left on any permutation is an operation that sets simultaneously. Likewise, a single cyclic shift to the right on any permutation is an operation that sets simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer — the size of the arrays.
The second line contains n integers — the elements of the first permutation.
The third line contains integers — the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
input
5
1 2 3 4 5
2 3 4 5 1
output
5
input
5
5 4 3 2 1
1 2 3 4 5
output
1
input
4
1 3 2 4
4 2 3 1
output
2
Note
For the first case: can be shifted to the right by . The resulting permutations will be $\{1,2,3,4,5\}$ and $\{1,2,3,4,5\}$.
For the second case: The operation is not required. For all possible rotations of and , the number of matching pairs won’t exceed .
For the third case: can be shifted to the left by . The resulting permutations will be ${1,3,2,4}$ and $\{2,3,1,4\}$. Positions and have matching pairs of elements. For all possible rotations of and , the number of matching pairs won’t exceed .
Translation
给两个长度为 的全排列 和 。处于两个排列中的数对 和 相等当且仅当 且 。可以对其中一个排列进行若干次 ,一次 的形式如下,将排列整体向左推,;或将排列整体向右推 。
问对其中一个排列进行若干次 后,相等数对的个数的最大值。
Idea
显然,在此题中,两个排列的运动是相对的,且左右对称;选择 或 操作结果是一样的,排列向左或向右推结果也是一样的;不妨假定,对 进行操作,并将 整体向右移动。
若 且 ,要使该数对相等, 需要移动 至位置 ;假如 ,移动步数为 ;否则,移动步数为 。设令数对 和 $b_j $ 相等的移动步数为 ,显然 ,多余 步的移动有一部分是无意义的。
设移动 步后相等数对的个数为 。对每一个 求出其需要移动的步数(可以记录全排列 中每个数的位置,就能轻易得到移动的步数),就能轻易得到 (每求的一个步数 , 增加 )。答案即为 。
Code
1 |
|
E
1 |
|