CF1110E Magic Stones

题目描述

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 2in1 ), 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+ci1ci .

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 2n105 ) — 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 0ci2109 ) — 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 0ti2109 ) — 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+ci1ci

是否能做若干次操作,使每个 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 2n105,0ci2109,0ti2109

输入方式:

行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

思路

  1. 首先 c 1 c_1 c1 ! ! != t 1 t_1 t1 c n c_n cn ! ! != t n t_n tn的情况肯定不可以
  2. 然后设 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 cici1,ai+1=ci+1ci
  3. c i ′ = c i + 1 + c i − 1 − c i c_i'=c_{i+1}+c_{i-1}-c_i ci=ci+1+ci1ci
  4. 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=cici1=ci+1+ci1cici1=ci+1ci=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+1ci=ci+1(ci+1+ci1ci)=cici1=ai
  5. 我们发现两个 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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

꧁Q༒ོγ꧂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值