http://codeforces.com/contest/1110/problem/E
Grigory has nn magic stones, conveniently numbered from 11 to nn . The charge of the ii -th stone is equal to cici .
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index ii , where 2≤i≤n−12≤i≤n−1 ), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge cici changes to c′i=ci+1+ci−1−cici′=ci+1+ci−1−ci .
Andrew, Grigory's friend, also has nn stones with charges titi . Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes cici into titi for all ii ?
Input
The first line contains one integer nn (2≤n≤1052≤n≤105 ) — the number of magic stones.
The second line contains integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤2⋅1090≤ci≤2⋅109 ) — the charges of Grigory's stones.
The second line contains integers t1,t2,…,tnt1,t2,…,tn (0≤ti≤2⋅1090≤ti≤2⋅109 ) — the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
题意:有一序列c(i),和t(i),问能否通过若干次操作将c变为t。每次操作是:选择一个i∈(1,n),改变c(i)为c(i-1)+c(i+1)-c(i)。
做法:建立差分数组d,d(i)=c(i+1)-c(i),然后对c(i)做一次操作d会发生如下变化:
即c(i)一次操作等价于交换d(i-1),d(i)位置。
那么c能转化为t等价于c的差分数组元素两两交换能变为t的差分数组 ,排个序就行了
差分数组相同自然原数组也相同
开始时特判一下c的第一个和最后一个元素必须等于t的第一个和最后一个。
#include<bits/stdc++.h>
using namespace std;
#define maxn 100000+100
int n,s[maxn],t[maxn],ds[maxn],dt[maxn];
int main()
{
// freopen("input.in","r",stdin);
cin>>n;
for(int i=1;i<=n;i++)cin>>s[i];
for(int i=1;i<=n;i++)cin>>t[i];
if(s[1]!=t[1]||s[n]!=t[n])puts("No");
else
{
for(int i=2;i<=n;i++)ds[i]=s[i]-s[i-1],dt[i]=t[i]-t[i-1];
sort(ds+2,ds+n+1);
sort(dt+2,dt+n+1);
for(int i=2;i<=n;i++)if(ds[i]!=dt[i])
{
puts("No");
exit(0);
}
puts("Yes");
}
return 0;
}