题意:给出长度为n的数组c和长度为n的数组t,已知对于数组c的每一项都可以使得c[i]=c[i+1]+c[i-1]−c[i],判断是否可以使得c数组变为t数组。
题解:c[i]-c[i-1]=c[i+1]-c[i] c[i+1]-c[i]=c[i]-c[i-1] 这个变换的意思其实是交换差分数组相邻的两项,所以写出c和t的差分数组排序后判断是否相同,且c1==t1即可。
#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=5e5+7;
typedef long long ll;
const int mod=1e7+7;
#define IO cin.tie(0),ios::sync_with_stdio(false);
#define pi acos(-1)
#define PII pair<ll,ll>
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
/** keep hungry and keep calm! **/
int a[maxn],b[maxn];
int Sheryang()
{
int n=read;
for(int i=1;i<=n;i++){
a[i]=read;
}
for(int i=1;i<=n;i++){
b[i]=read;
}
vector<int>v1,v2;
for(int i=2;i<=n;i++){
v1.push_back(a[i]-a[i-1]);
v2.push_back(b[i]-b[i-1]);
}
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
if(v1==v2 && a[1]==b[1]){
printf("YES\n");
}else{
printf("No\n");
}
return 0;
}