题意:有一个n行m列的矩阵,有0和1两个状态, Ashish 和Vivek每次可以走一步将0变为1,Ashish先走,每一步都需要保证当前所在行和列全为0,问最终谁赢。
题解:只需要求出有多少行为0和有多少列为0,最后求其最小值,就是可以走的总步数。为奇数时Ashish赢,反之Vivek赢。
#include
#include
#include
#include
#include
#include
#include
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;
}
英语不好真的是硬伤呀,我做的时候以为a,b数组必须对应且b数组必须不同。
其实这道题以上是只要b数组中有0和1就行,所以只需要考虑3种情况全为1 全为0 有1有0。
#include
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;
}
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
#include
#include
#include
#include
#include
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;
}