Magic Stones CodeForces - 1110E (思维+差分)

E. Magic Stones

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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".

Examples

input

Copy

4
7 2 4 12
7 15 10 12

output

Copy

Yes

input

Copy

3
4 4 4
1 2 3

output

Copy

No

Note

In the first example, we can perform the following synchronizations (11-indexed):

  • First, synchronize the third stone [7,2,4,12]→[7,2,10,12][7,2,4,12]→[7,2,10,12].
  • Then synchronize the second stone: [7,2,10,12]→[7,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

思路:

通过样例观察:

In the first example, we can perform the following synchronizations (11-indexed):

    • First, synchronize the third stone [7,2,4,12]→[7,2,10,12][7,2,4,12]→[7,2,10,12].
    • Then synchronize the second stone: [7,2,10,12]→[7,15,10,12][7,2,10,12]→[7,15,10,12

我们来看最初的数组,和中途的数组,以及目标数组,他们的差分都是【5,8,2】这三个数,变来变去都是这三个,

再加以观察可以发现,我们每执行一个操作,影响的只是交换了差分,那么只需要数组的首尾两个数相等,并且中间的差分数排序后相等即可保证一一定能交换成功。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int b[maxn];
int main()
{
    gbtb;
    cin>>n;
    repd(i,1,n)
    {
        cin>>a[i];
    }
    repd(i,1,n)
    {
        cin>>b[i];
    }

    std::vector<int> v1;
    std::vector<int> v2;
    bool isok=1;
    if(a[1]!=b[1]||a[n]!=b[n])
    {
        // db(2);
        isok=0;
    }

    repd(i,2,n)
    {
        v1.pb(a[i]-a[i-1]);
        v2.pb(b[i]-b[i-1]);
    }
    int z=sz(v1);
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    repd(i,0,z-1)
    {
        if(v1[i]!=v2[i])
        {
            isok=0;
        }
    }
    if(isok)
    {
        printf("Yes\n");
    }else
    {
        printf("No\n");
    }
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ‘ ‘ || ch == ‘\n‘);
    if (ch == ‘-‘) {
        *p = -(getchar() - ‘0‘);
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 - ch + ‘0‘;
        }
    }
    else {
        *p = ch - ‘0‘;
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 + ch - ‘0‘;
        }
    }
}

原文地址:https://www.cnblogs.com/qieqiemin/p/10356180.html

时间: 2024-11-03 21:08:23

Magic Stones CodeForces - 1110E (思维+差分)的相关文章

Codeforces 1110E (差分)

题面 传送门 分析 一开始考虑贪心和DP,发现不行 考虑差分: 设d[i]=c[i+1]-c[i] (i<n) 那么一次操作会如何影响差分数组呢? \(c[i]'=c[i+1]+c[i-1]-c[i]\) \(d'[i-1]=c[i]'-c[i-1]=c[i+1]-c[i]=d[i]\) \(d'[i]=c[i]-c[i-1]=d[i-1]\) 我们发现操作只是改变了差分数组中不同值的位置 因此,我们只要求出c,t对应的差分数组,并比较它们是否相同即可 注意由于c[1]和c[n]不能变,所以要判

E. Magic Stones CF 思维题

E. Magic Stones time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Grigory has nn magic stones, conveniently numbered from 11 to nn . The charge of the ii -th stone is equal to cici . Sometime

【CF1110E】 Magic Stones - 差分

题面 Grigory has n n magic stones, conveniently numbered from \(1\) to \(n\). The charge of the \(i\)-th stone is equal to \(c_i\). Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index \(i\), where \(2 \le i \le n-1

CF 1110 E. Magic Stones

E. Magic Stones 链接 题意: 给定两个数组,每次可以对一个数组选一个位置i($2 \leq i \leq n - 1$),让a[i]=a[i-1]+a[i+1]-a[i],或者b[i]=b[i-1]+b[i+1]-b[i].问进行一些操作后,a和b能否相同. 分析: 考虑一次操作会变成什么样子. a b c a a+c-b c 我们发现这些数字差分后是不变的.于是对两个数组差分后,排序,看是否一样即可.注意判一下1和n是否相等. 代码: #include<cstdio> #in

Dima and Magic Guitar CodeForces - 366E

Dima and Magic Guitar CodeForces - 366E 题意: http://blog.csdn.net/u011026968/article/details/38716425http://vawait.com/2013/11/codeforces-366e/http://www.cnblogs.com/jianglangcaijin/archive/2013/11/25/3441319.html 对于s中任意相邻两个数x和y,都要求在矩形中找出任意两个分别等于x和y的点

【Codeforces Global Round 1 E】Magic Stones

[链接] 我是链接,点我呀:) [题意] 你可以把c[i]改成c[i+1]+c[i-1]-c[i] (2<=i<=n-1) 问你能不能把每一个c[i]都换成对应的t[i]; [题解] d[i] = c[i+1]-c[i]; (1<=i<=n-1) change c[i] c[i]' = c[i+1]+c[i-1]-c[i]; d[i-1] = c[i]'-c[i-1]; = c[i+1]+c[i-1]-c[i]-c[i-1] == c[i+1]-c[i] = d[i]; d[i]

Magic Stones(思维题)

#include <bits/stdc++.h> using namespace std; typedef long long ll; int v1[100001]; int v2[100001]; int s1[100001]; int s2[100001]; int main () { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> v1[i]

「日常训练」Magic Stones(CodeForces-1110E)

题意 给定两个数组c和t,可以对c数组中的任何元素变换\(c_i\)?成\(c_{i+1}+c_{i-1}-c_i\)?,问c数组在若干次变换后能否变换成t数组. 分析 这种魔法题目我是同样的没做过.Editorial里说用差分来能够看出思路.问题是,如何能够想到差分来做?因为它的变换就是相邻的数的加减法,那么想要找到思路的突破口必须也得从这里突破. 考虑变换前后的数组: 原来:\(a_{i-1}, a_i, a_{i+1}\) 之后:\(a_{i-1}, a_{i-1}+a_{i+1}-a_i

CodeForces 731D (差分+线段扫描)

Description Archeologists have found a secret pass in the dungeon of one of the pyramids of Cycleland. To enter the treasury they have to open an unusual lock on the door. The lock consists of n words, each consisting of some hieroglyphs. The wall ne