Codeforces Round #648 (Div. 2)—CodeForces1365
文章目录
1365B-Trouble Sort
https://codeforces.com/problemset/problem/1365/B
Problem:
将两种数字进行排序,只有不同种数字间可以交换。问若干次交换后是否可以成为非降序排列
Solution:
全为种类0或种类1,这样由于不能交换,只需要判断是否降序
否则种类0和种类1都存在时一定可以做到
Code:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll;
inline ll read() {
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
return x*f;
}
const int maxn=1e6+5;
const ll mod=1e9+7;
int t;
int n;
int a[maxn],b[maxn],cnt[5];
int main() {
t=read();
a[0]=0;
while(t--) {
n=read();
bool f1=true;
cnt[0]=cnt[1]=0;
for(int i=1;i<=n;i++){
a[i]=read();
if(a[i]<a[i-1]) f1=false;
}
for(int i=1;i<=n;i++){
b[i]=read();
cnt[b[i]]=1;
}
if(f1) cout<<"Yes"<<endl;
else if(cnt[0]&&cnt[1])cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
1365C- Rotation Matching
https://codeforces.com/problemset/problem/1365/C
Problem:
给两段序列 可以随意左移右移 问最多a b有多少个数 在同一个位置相同
Solution:
拿第一个序列作为模板,记录位置。第二个序列比对,记录累加偏移值。累计偏移值的最大值为答案
Code:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll;
inline ll read() {
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
return x*f;
}
const int maxn=1e6+5;
const ll mod=1e9+7;
int t;
int n;
int a[maxn],b[maxn],pos[maxn],cnt[maxn];
int main() {
// t=read();
// while(t--) {
n=read();
for(int i=1; i<=n; i++) {
a[i]=read();
pos[a[i]]=i;
}
for(int i=1;i<=n;i++){
b[i]=read();
cnt[(pos[b[i]]-i+n)%n]++;
}
int ans=0;
for(int i=0;i<n;i++){
ans=max(ans,cnt[i]);
}
cout<<ans<<endl;
// }
return 0;
}
1365F-Swaps Again
https://codeforces.com/problemset/problem/1365/F
Problem:
对于一个长度为n的序列,可以执行以下操作:
选择一个正整数k
将序列前k个与后k个数交换位置
有两个长度相同的数组a,b。能对a进行任意次上述操作,能否使两个数组相等。
Solution:
如果两个位置是对称的,那么他们永远是对称的。因此只要把对称位置的变成一组,比较两个数组的所有组是否相等,如果相等就可以
Code:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll;
inline ll read() {
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
return x*f;
}
const int maxn=1e6+5;
const ll mod=1e9+7;
int t;
int n;
int a[maxn],b[maxn];
pair<int,int>p1[maxn],p2[maxn];
int main() {
t=read();
while(t--){
n=read();
memset(p1,0,sizeof(p1));
memset(p2,0,sizeof(p2));
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<=n;i++) b[i]=read();
if((n&1)&&a[n/2+1]!=b[n/2+1]){
cout<<"No"<<endl;
continue;
}
for(int i=1;i<=n/2;i++){
p1[i].first=min(a[i],a[n-i+1]);
p1[i].second=max(a[i],a[n-i+1]);
p2[i].first=min(b[i],b[n-i+1]);
p2[i].second=max(b[i],b[n-i+1]);
}
sort(p1+1,p1+n/2+1);
sort(p2+1,p2+n/2+1);
bool flag=true;
for(int i=1;i<=n;i++){
if(p1[i].first!=p2[i].first||p1[i].second!=p2[i].second){
flag=false;
break;
}
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}