#648 (Div. 2)(A. Matrix Game、B. Trouble Sort、C. Rotation Matching)

Codeforces Round #648 (Div. 2)(A、B、C)

A. Matrix Game

题意:有一个n行m列的矩阵,有0和1两个状态, Ashish 和Vivek每次可以走一步将0变为1,Ashish先走,每一步都需要保证当前所在行和列全为0,问最终谁赢。
题解:只需要求出有多少行为0和有多少列为0,最后求其最小值,就是可以走的总步数。为奇数时Ashish赢,反之Vivek赢。

#include<iostream>
#include<algorithm>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int t,n,m,sum;
int a[60][60];
int b[60];
int main()
{
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		sum=0;
		int k=0,q=0;
		memset(b,0,sizeof b);
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		cin>>a[i][j];
			for(int i=0;i<n;i++){
				sum=0;
		 for(int j=0;j<m;j++){
		 	if(a[i][j]==0)  sum++;
		 	else if(a[i][j]==1&&b[j]==0) q++,b[j]=1;
		 }
		if(sum==m)   k++;		
			}
			k=min((m-q),k);
	if(k%2==0) cout<<"Vivek\n";
	else cout<<"Ashish\n";
	}
	return 0;
 } 
B. Trouble Sort

英语不好真的是硬伤呀,我做的时候以为a,b数组必须对应且b数组必须不同。
其实这道题以上是只要b数组中有0和1就行,所以只需要考虑3种情况全为1 全为0 有1有0。

#include <iostream>
using namespace std;
typedef long long ll;
int k,q,w,t,n;
int a[505],b[505];
int main()
{
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=0;i<n;i++)
		     cin>>a[i];
		     w=0;
			 q=0;
		for(int i=0;i<n;i++)
		  {
		      cin>>b[i];
		      if(b[i]==0)  q++;
		      else w++;
	}
	int t=1;
	if(q==n||q==0){
		for(int i=1;i<n;i++)
		if(a[i]<a[i-1]){
			t=0;
			break;
		}
		if(t) cout<<"Yes\n";
		else cout<<"No\n";
	}
	else cout<<"Yes\n";
	}
	return 0;
 }
C. Rotation Matching

outputstandard output
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 sizen.Let’s call themaandb.

Note that a permutation ofnelements is a sequence of numbersa1,a2,…,an, in which every number from1tonappears exactly once.

The message can be decoded by an arrangement of sequenceaandb, such that the number of matching pairs of elements between them is maximum.A pair of elementsaiandbjis 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 numberkand cyclically shift one of the permutations to the left or rightktimes.
A single cyclic shift to the left on any permutationcis an operation that setsc1:=c2,c2:=c3,…,cn:=c1simultaneously.Likewise, a single cyclic shift to the right on any permutationcis an operation that setsc1:=cn,c2:=c1,…,cn:=cn−1simultaneously.

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 integern (1≤n≤2⋅105)— the size of the arrays.

The second line containsnintegersa1, a2, …, an (1≤ai≤n)— the elements of the first permutation.

The third line containsnintegersb1, 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.
题解:将b[]中的每个数字与a[]中相同数字的位置一一对应,计算需要位移多少步达到对应,并记录此时步数的对应个数(用ans[]来记录此时对应个数),**注意左移y位与右移(n-y)位结果是相同的!!!**最后max取最大值即可。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;
int a[200010],ans[200005];
int main()
{
	int n,x;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d",&x);
		a[x]=i;//记录a数组中每个数字的下标位置
	}
	int t=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		x=(a[x]-i+n)%n;//在a[]与b[]相同的条件下,
		        //计算需要位移多少步。
		ans[x]++;//当前步数对应个数加1
		t=max(ans[x],t);//取最大值
	}
	cout<<t<<endl;
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Think-killer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值