Codeforces Round #732 (Div. 2) ABC题解
链接:
A-AquaMoon and Two Arrays
题意:给你两个数组a[],b[],每次选择两个下标i,j,则a[i]-1,a[j]+1。问你操作多少次使a[]变成b[],并输出操作过程的i和j的值,如果不存在,则输出-1。 思路:类似于双指针,index1=1,index2=1遍历两个数组,再另外开两个数组存储index1和index2的值。当ans != sum 时,输出-1。 代码:
#include<bits/stdc++.h>
using namespace std;
int t,n;
int a[105],b[105],c[105],d[105];
int main(){
cin>>t;
while(t--){
cin>>n;
int sum=0,ans=0;
for(int i=1; i <= n; i++){
cin>>a[i];
sum+=a[i];
}
for(int i=1; i <= n; i++){
cin>>b[i];
ans+=b[i];
}
if(ans != sum){
cout<<"-1"<<endl;
continue;
}
int k=0,i=1,j=1;
while(i <= n && j <= n){
if(a[i] >= b[i]) i++;
if(a[j] <= b[j]) j++;
while(a[i] < b[i] && a[j] > b[j]){
a[i]++;a[j]--;
k++;
c[k]=j;d[k]=i;
}
}
cout<<k<<endl;
for(int i=1; i <= k; i++){
cout<<c[i]<<" "<<d[i]<<endl;
}
}
return 0;
}
B- AquaMoon and Stolen String
题意:字符串之间同下标可以交换字母,找出丢失的字符串。 思路:暴力枚举,对于每一列,丢失前字母之和-丢失后字母之和=该列的字母。 代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int t;
int n,m;
int a[200];
string s[N],c[N];
int main(){
cin>>t;
while(t--){
cin>>n>>m;
for(int i=1; i <= n ; i++) cin>>s[i];
for(int i=1 ;i <= n-1; i++) cin>>c[i];
int i=0;
while(i < m){
int sum=0,ans=0;
for(int j=1; j <= n; j++){
sum += s[j][i];
}
for(int j=1; j <= n-1; j++){
ans += c[j][i];
}
cout<<(char) (sum - ans);
i++;
}
cout<<endl;
}
return 0;
}
C-AquaMoon and Strange Sort
题意:排序之前所有人是向右的,相邻两人可以交换位置,交换后他们的方向也变化,判断是否满足从小到大排序之后所有人还是是向右的。 思路:所有人的交换次数一定是偶数才满足条件,分下标奇偶数讨论 代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int t,n;
int a[N],odd[N],even[N];
int main(){
cin>>t;
while(t--){
cin>>n;
memset(odd,0,sizeof odd);
memset(even,0,sizeof even);
for(int i = 1; i <= n; i++) cin>>a[i];
for(int i = 1; i <= n; i++){
if(i % 2) odd[a[i]]++;
else even[a[i]]++;
}
sort(a+1,a+1+n);
for(int i = 1; i <= n; i++){
if(i % 2) odd[a[i]]--;
else even[a[i]]--;
}
int flag=1;
for(int i = 1; i <= n; i++){
if(odd[a[i]] < 0 || even[a[i]] < 0){
flag=0;
break;
}
}
if(flag) puts("Yes");
else puts("No");
}
return 0;
}
