Codeforces Round #648 (Div. 2)

题目

    • A. Matrix Game
    • B. Trouble Sort
    • C. Rotation Matching
    • D. Solve The Maze

A. Matrix Game

Ashish and Vivek play a game on a matrix consisting of n rows and m columns, where they take turns claiming cells. Unclaimed cells are represented by 0, while claimed cells are represented by 1. 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 t (1≤t≤50) — 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 n, m (1≤n,m≤50) — the number of rows and columns in the matrix.
The following n lines consist of m integers each, the j-th integer on the i-th line denoting ai,j (ai,j∈{0,1}).
Output
For each test case if Ashish wins the game print “Ashish” otherwise print “Vivek” (without quotes).
Example
inputCopy
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
outputCopy
Vivek
Ashish
Vivek
Ashish
Note
For the first case: One possible scenario could be: Ashish claims cell (1,1), Vivek then claims cell (2,2). Ashish can neither claim cell (1,2), nor cell (2,1) as cells (1,1) and (2,2) 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 (1,1), 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.
题意
给定一个n*m的单元格矩阵,每个单元格中的值只能是1或者0,Vivek和Ashish游戏,可以把任意一个1变成0,如果其中一个人把某个单元格的0变成了1就,那么就表示占领这个单元格所在的行和所在列,谁最后不能继续占领谁就输了,输出胜利的一方
思路
两者都是选择最优解,只要求出目前未被min(占领的行,未被占领的列的)如果是奇数,则Ashish胜利

int a[210][210];
bool flag[100];
int main()
{
	std::ios::sync_with_stdio(false);
    std::cin.tie(0);
	int t,n,m,x,y,u,v;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		int r=0,l=0;//行列 
		memset(flag,false,sizeof(flag));
		for(int i=1;i<=n;i++)
		{
			int row=0; 
			for(int j=1;j<=m;j++)
			{
				cin>>a[i][j];
				if(a[i][j]==1){
					row=1;
					flag[j]=true;
				}
			}
			if(row==1)r++;
		}
		for(int i=1;i<=m;i++)
		{
			if(flag[i])l++;
		}
		int ans;
		if(r==0&&l==0)
		{
			ans=min(n,m);
			if(ans%2==0)
			{
				cout<<"Vivek"<<endl;
			}
			else
			{
				cout<<"Ashish"<<endl;
			}
		}
		else 
		{
			ans=min(n-r,m-l);
			if(ans%2==0)
			{
				cout<<"Vivek"<<endl;
			}
			else
			{
				cout<<"Ashish"<<endl;
			}
		}
	}
	return 0;
}

B. Trouble Sort

Ashish has n elements arranged in a line.
These elements are represented by two integers ai — the value of the element and bi — the type of the element (there are only two possible types: 0 and 1). He wants to sort the elements in non-decreasing values of ai.
He can perform the following operation any number of times:
Select any two elements i and j such that bi≠bj 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 t (1≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer n (1≤n≤500) — the size of the arrays.
The second line contains n integers ai (1≤ai≤105) — the value of the i-th element.
The third line containts n integers bi (bi∈{0,1}) — the type of the i-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
inputCopy
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
outputCopy
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 1 and 2, then swap elements at positions 2 and 3.
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 i and j such that bi≠bj. The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions 3 and 4, then elements at positions 1 and 2.
题意
给定n个数,这n个数的值为ai,属性为bi(0或者1),可以交换任意两个属性不同的数无数次,如果交换之后可以使得数列按照升序排序,就输出YES
思路
只要即存在属性为0的数,同时存在属性为1的数,就可以把每个数交换到任意位置,达到升序序列,所以既有0又有1就输出YES

struct node{
	int x;
	int y;
}a[520];
int b[520];
int main()
{
	std::ios::sync_with_stdio(false);
    std::cin.tie(0);
	int t,n,m,x,y,u,v;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i].x;
			b[i]=a[i].x;
		} 
		for(int i=1;i<=n;i++){
			cin>>a[i].y;
		} 
		sort(b+1,b+1+n);
		int flag=1;
		for(int i=1;i<=n;i++)
		{
			if(a[i].x!=b[i]){
				flag=0;
			}
		}
		int t1=0,t2=0;
		if(flag==1){
			printf("YES\n");
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			if(a[i].y==0)t1=1;
			if(a[i].y==1)t2=1;
		}
		if(t1==1&&t2==1)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

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 n. Let’s call them a and b.
Note that a permutation of n elements is a sequence of numbers a1,a2,…,an, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements ai and bj is said to match if:
i=j, that is, they are at the same index.
ai=bj
His two disciples are allowed to perform the following operation any number of times:
choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c1:=c2,c2:=c3,…,cn:=c1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c1:=cn,c2:=c1,…,cn:=cn−1 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 n (1≤n≤2⋅105) — the size of the arrays.
The second line contains n integers a1, a2, …, an (1≤ai≤n) — the elements of the first permutation.
The third line contains n integers b1, b2, …, bn (1≤bi≤n) — 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
inputCopy
5
1 2 3 4 5
2 3 4 5 1
outputCopy
5
inputCopy
5
5 4 3 2 1
1 2 3 4 5
outputCopy
1
inputCopy
4
1 3 2 4
4 2 3 1
outputCopy
2
Note
For the first case: b can be shifted to the right by k=1. 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 a and b, the number of matching pairs won’t exceed 1.
For the third case: b can be shifted to the left by k=1. The resulting permutations will be {1,3,2,4} and {2,3,1,4}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won’t exceed 2.
题意
有两个长度相同的数列a和b,可以把a、b数列左移或者右移(环),求移动之后a、b数列下标相同的且值相同的数最多有多少
思路
求出a、b数组中相同的数的位置差,答案就是位置差最多的数。

int a[200010],b[200010];
int la[200010],lb[200010];
int t[200010];
int main()
{
	std::ios::sync_with_stdio(false);
    std::cin.tie(0);
	int n,m,x,y,u,v;
		cin>>n;
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			la[a[i]]=i;	
		}
		for(int i=1;i<=n;i++)
		{
			cin>>b[i];
			lb[b[i]]=i;
			x=(lb[b[i]]-la[b[i]]+n)%n;
			t[x]++;
		}
		sort(t,t+1+n);
		cout<<t[n]<<endl;
	return 0;
}

D. Solve The Maze

Vivek has encountered a problem. He has a maze that can be represented as an n×m grid. Each of the grid cells may represent the following:
Empty — ‘.’
Wall — ‘#’
Good person — ‘G’
Bad person — ‘B’
The only escape from the maze is at cell (n,m).
A person can move to a cell only if it shares a side with their current cell and does not contain a wall. Vivek wants to block some of the empty cells by replacing them with walls in such a way, that all the good people are able to escape, while none of the bad people are able to. A cell that initially contains ‘G’ or ‘B’ cannot be blocked and can be travelled through.
Help him determine if there exists a way to replace some (zero or more) empty cells with walls to satisfy the above conditions.
It is guaranteed that the cell (n,m) is empty. Vivek can also block this cell.
Input
The first line contains one integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n, m (1≤n,m≤50) — the number of rows and columns in the maze.
Each of the next n lines contain m characters. They describe the layout of the maze. If a character on a line equals ‘.’, the corresponding cell is empty. If it equals ‘#’, the cell has a wall. ‘G’ corresponds to a good person and ‘B’ corresponds to a bad person.
Output
For each test case, print “Yes” if there exists a way to replace some empty cells with walls to satisfy the given conditions. Otherwise print “No”
You may print every letter in any case (upper or lower).
Example
inputCopy
6
1 1
.
1 2
G.
2 2
#B
G.
2 3
G.#
B#.
3 3
#B.
#…
GG.
2 2
#B
B.
outputCopy
Yes
Yes
No
No
Yes
Yes
Note
For the first and second test cases, all conditions are already satisfied.
For the third test case, there is only one empty cell (2,2), and if it is replaced with a wall then the good person at (1,2) will not be able to escape.
For the fourth test case, the good person at (1,1) cannot escape.
For the fifth test case, Vivek can block the cells (2,3) and (2,2).
For the last test case, Vivek can block the destination cell (2,2).

你可能感兴趣的:(codeforces)