题目
题意翻译
题意:
一次操作选择1<i<n1<i<n,使c_ic
i
变为c_i’c
i
′
,c_i’=c_{i+1}+c_{i-1}-c_ic
i
′
=c
i+1
+c
i−1
−c
i
是否能做若干次操作,使每个c_ic
i
和t_it
i
相等?
数据范围:
2\le n\le 10^5, 0\le c_i\le 210^9, 0\le t_i\le 210^92≤n≤10
5
,0≤c
i
≤2∗10
9
,0≤t
i
≤2∗10
9
输入方式:
行1 nn
行2 c_1, \cdots, c_nc
1
,⋯,c
n
行3 t_1, \cdots, t_nt
1
,⋯,t
n
输出方式:
Yes/No
题目描述
Grigory has n n magic stones, conveniently numbered from 1 1 to n n . The charge of the i i -th stone is equal to c_i c
i
.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i i , where 2 \le i \le n - 1 2≤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 c_i c
i
changes to c_i’ = c_{i + 1} + c_{i - 1} - c_i c
i
′
=c
i+1
+c
i−1
−c
i
.
Andrew, Grigory’s friend, also has n n stones with charges t_i t
i
. 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 c_i c
i
into t_i t
i
for all i i ?
输入输出格式
输入格式:
The first line contains one integer n n ( 2 \le n \le 10^5 2≤n≤10
5
) — the number of magic stones.
The second line contains integers c_1, c_2, \ldots, c_n c
1
,c
2
,…,c
n
( 0 \le c_i \le 2 \cdot 10^9 0≤c
i
≤2⋅10
9
) — the charges of Grigory’s stones.
The second line contains integers t_1, t_2, \ldots, t_n t
1
,t
2
,…,t
n
( 0 \le t_i \le 2 \cdot 10^9 0≤t
i
≤2⋅10
9
) — the charges of Andrew’s stones.
输出格式:
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print “Yes”.
Otherwise, print “No”.
输入输出样例
输入样例#1:
4
7 2 4 12
7 15 10 12
输出样例#1:
Yes
输入样例#2:
3
4 4 4
1 2 3
输出样例#2:
No
说明
In the first example, we can perform the following synchronizations ( 1 1 -indexed):
First, synchronize the third stone [7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12] [7,2,4,12]→[7,2,10,12] .
Then synchronize the second stone: [7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12] [7,2,10,12]→[7,15,10,12] .
In the second example, any operation with the second stone will not change its charge.
思路
差分数组
首先做c的差分数组a
对于每次操作c[i]=c[i-1]+c[i+1]我们可以理解成c[i]+=c[i-1]-c[i]+c[i+1]
然后根据变化做差分数组,发现一次操作就相当于调换a[i]和a[i+1] 的位置,所以只需要比较t和c的差分数组是否相同即可
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5+77;
int n,a[maxn],b[maxn],c[maxn],d[maxn];
int main()
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
for(int i=1; i<=n; i++) scanf("%d",&b[i]);
if(b[1]!=a[1]||a[n]!=b[n])
{
printf("No"); return 0;
}
for(int i=1; i<=n; i++) c[i]=a[i]-a[i-1],d[i]=b[i]-b[i-1];
sort(c+1,c+n+1); sort(d+1,d+n+1);
for(int i=1; i<=n; i++) if(c[i]!=d[i])
{
printf("No"); return 0;
}
printf("Yes");
}