题目描述
Grigory has n n n magic stones, conveniently numbered from 1 1 1 to n n n . The charge of the i i i -th stone is equal to c i c_i ci .
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i i i , where 2 ≤ i ≤ n − 1 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 ci changes to c i ′ = c i + 1 + c i − 1 − c i c_i' = c_{i + 1} + c_{i - 1} - c_i ci′=ci+1+ci−1−ci .
Andrew, Grigory’s friend, also has n n n stones with charges t i t_i ti . 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 ci into t i t_i ti for all i i i ?
输入格式
The first line contains one integer n n n ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2≤n≤105 ) — the number of magic stones.
The second line contains integers c 1 , c 2 , … , c n c_1, c_2, \ldots, c_n c1,c2,…,cn ( 0 ≤ c i ≤ 2 ⋅ 1 0 9 0 \le c_i \le 2 \cdot 10^9 0≤ci≤2⋅109 ) — the charges of Grigory’s stones.
The second line contains integers t 1 , t 2 , … , t n t_1, t_2, \ldots, t_n t1,t2,…,tn ( 0 ≤ t i ≤ 2 ⋅ 1 0 9 0 \le t_i \le 2 \cdot 10^9 0≤ti≤2⋅109 ) — 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
样例输入 #1
4
7 2 4 12
7 15 10 12
样例输出 #1
Yes
样例 #2
样例输入 #2
3
4 4 4
1 2 3
样例输出 #2
No
提示
In the first example, we can perform the following synchronizations ( 1 1 1 -indexed):
- First, synchronize the third stone [ 7 , 2 , 4 , 12 ] → [ 7 , 2 , 10 , 12 ] [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 , 2 , 10 , 12 ] → [ 7 , 15 , 10 , 12 ] [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.
题面翻译
题意:
一次操作选择 1 < i < n 1<i<n 1<i<n,使 c i c_i ci变为 c i ′ c_i' ci′, c i ′ = c i + 1 + c i − 1 − c i c_i'=c_{i+1}+c_{i-1}-c_i ci′=ci+1+ci−1−ci
是否能做若干次操作,使每个 c i c_i ci和 t i t_i ti相等?
数据范围:
2 ≤ n ≤ 1 0 5 , 0 ≤ c i ≤ 2 ∗ 1 0 9 , 0 ≤ t i ≤ 2 ∗ 1 0 9 2\le n\le 10^5, 0\le c_i\le 2*10^9, 0\le t_i\le 2*10^9 2≤n≤105,0≤ci≤2∗109,0≤ti≤2∗109
输入方式:
行1 n n n
行2 c 1 , ⋯ , c n c_1, \cdots, c_n c1,⋯,cn
行3 t 1 , ⋯ , t n t_1, \cdots, t_n t1,⋯,tn
输出方式:
Yes/No
思路
- 首先 c 1 c_1 c1 ! ! != t 1 t_1 t1或 c n c_n cn ! ! != t n t_n tn的情况肯定不可以
- 然后设 a i = a_i= ai= c i − c i − 1 , a i + 1 = c i + 1 − c i c_i-c_{i-1},a_{i+1}=c_{i+1}-c_i ci−ci−1,ai+1=ci+1−ci
- c i ′ = c i + 1 + c i − 1 − c i c_i'=c_{i+1}+c_{i-1}-c_i ci′=ci+1+ci−1−ci
- 则
a
i
=
c
i
′
−
c
i
−
1
=
c
i
+
1
+
c
i
−
1
−
c
i
−
c
i
−
1
=
c
i
+
1
−
c
i
=
a
i
+
1
a_i=c_i'-c_{i-1}=c_{i+1}+c_{i-1}-c_i-c_{i-1}=c_{i+1}-c_i=a_{i+1}
ai=ci′−ci−1=ci+1+ci−1−ci−ci−1=ci+1−ci=ai+1
a i + 1 = c i + 1 − c i ′ = c i + 1 − ( c i + 1 + c i − 1 − c i ) = c i − c i − 1 = a i a_{i+1}=c_{i+1}-c_i'=c_{i+1}-(c_{i+1}+c_{i-1}-c_i)=c_i-c_{i-1}=a_{i} ai+1=ci+1−ci′=ci+1−(ci+1+ci−1−ci)=ci−ci−1=ai - 我们发现两个 a i a_i ai 互换了。于是问题就变成了:给两个差分数组,每次操作可以将其中一个数组的相邻两个元素互换,问能不能让两个数组相等。
代码
/*************************************************
Note:
*************************************************/
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <iomanip>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
inline int read()
{
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
int a[N], b[N], c[N], d[N];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
if (a[1] != b[1] || a[n] != b[n])
{
cout << "No" << endl;
return 0;
}
for (int i = 1; i < n; i++)
{
c[i] = a[i + 1] - a[i];
d[i] = b[i + 1] - b[i];
}
sort(c + 1, c + n);
sort(d + 1, d + n);
for (int i = 1; i < n; i++)
if (c[i] != d[i])
{
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
return 0;
}