Codeforces Contest 1110 problem E Magic Stones —— 更改算式

Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to ci.

Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 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 ci changes to c′i=ci+1+ci−1−ci.

Andrew, Grigory’s friend, also has n stones with charges 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 ci into ti for all i?

Input
The first line contains one integer n (2≤n≤105) — the number of magic stones.

The second line contains integers c1,c2,…,cn (0≤ci≤2⋅109) — the charges of Grigory’s stones.

The second line contains integers t1,t2,…,tn (0≤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”.

Examples
inputCopy
4
7 2 4 12
7 15 10 12
outputCopy
Yes
inputCopy
3
4 4 4
1 2 3
outputCopy
No
Note
In the first example, we can perform the following synchronizations (1-indexed):

First, synchronize the third stone [7,2,4,12]→[7,2,10,12].
Then synchronize the second stone: [7,2,10,12]→[7,15,10,12].
In the second example, any operation with the second stone will not change its charge.

题意:

给你n个数,你可以在2到n-1之间选任意的数,让这个数a[x]变成a[x-1]+a[x+1]-a[x],问你最后能不能将这个数组变成接下来给你的数组。

题解:

也是想得到就很简单,想不到就凉凉的题目,a[x]’ = a[x-1]+a[x+1]-a[x]可以变成a[x]’-a[x-1]=a[x+1]-a[x],a[x+1]-a[x]’=a[x]-a[x-1],也就是将相邻的两个差交换一下。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int c[N],t[N],dif1[N],dif2[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&c[i]),dif1[i]=c[i]-c[i-1];
    for(int i=1;i<=n;i++)
        scanf("%d",&t[i]),dif2[i]=t[i]-t[i-1];
    sort(dif1+1,dif1+1+n);
    sort(dif2+1,dif2+1+n);
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        if(dif1[i]!=dif2[i])
        {
            flag=1;
            break;
        }
    }
    if(!flag)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/88319135