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 .

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.

思路:

注意看题目给出的公式

c[i]‘=c[i+1]+c[i-1]-c[i]

可以变形成

c[i+1]-c[i]‘=c[i]-c[i-1]

c[i]‘-c[i-1]=c[i+1]-c[i]

这样看来就是将第i项左右差分交换,可以把这个看成一种排序

所以将差分全部重新排序之后,如果上下差分都相同,则满足题目要求

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
ll a[maxn],b[maxn],c[maxn],d[maxn];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%I64d",&b[i]);
    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);
    if(a[1]!=b[1]||a[n]!=b[n])
    {
        printf("No\n");
        return 0;
    }
    for(int i=1;i<n;i++)
    {
        if(c[i]!=d[i])
        {
            printf("No\n");
            return 0;
        }
    }
    printf("Yes\n");
    return 0;
}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10357305.html

时间: 2024-11-13 01:27:28

E. Magic Stones CF 思维题的相关文章

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 G

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

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &

【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

Unique Encryption Keys (思维题 预处理)

题目 题意:给m个数字, q次询问, 询问b到e之间如果有重复数字就输出, 没有就输出OK 思路:用f[i]数组 记录从i开始向后最近的有重复数字的 位置, 如 1 3 2 2, 则f[1] = 4; 如果离a最近的重复数字的位置 都大于b, 就说明没有重复数字. f[]数组需要预处理,从后向前. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector>

sdut 2847 Monitor (思维题)

题目 题意:给定a, b, x, y;  求使c, d; 使c:d = x :y; 且c<=a, d<=b, 而且c, d尽量大. 先求最小倍数, 再用最小倍数乘 x, y; 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 long long gcd(long long a, l

学习方法_2011年编写和锻炼的思维题

1.多看,多练,多想,多总结,最重要就是不停的写代码! 给自己的目标:一天代码量最少800行,无论是什么代码,如果练习的量不够,就重复做已经写过的代码. 思维题: 找出这当中数字 1,11,31,4113,612314 的规律是怎样的? 1,11,表示前面的数有1个131,表示前面所有的数有3个14113,表示前面的所有的数有4个1.1个3以此类推,前面所有的数有6个1.2个3.1个4,即为612314 1.两个无窗的房间,其中一间有三个电灯,另一间里面有三个开关,三个开关各控制三个电灯.问:每

思维题 URAL 1718 Rejudge

题目传送门 1 /* 2 题意:数据加10组,再删掉第6组数据,问rejudge后最少最多几个作者收到邮件 3 思维题:当错在6时结果是不一定,错在7时是一定改变,因为会变成6 4 思路没错,但用结构题排序一直WA,代码有毒!学习使用set容器. 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 #include <str

ZOJ 3829 贪心 思维题

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题,自己智商不够,不敢搞,想着队友智商好,他们搞吧,但是没出来这题...... 以后任何时候,都自信点....该想的还是好好自己想,这类题感觉就是先去找性质,然后一点点找规律,如果必要的话,自己提出一点猜想,然后如果自己举不出来反例,就暂时认为是正确的 下午搞了一下午,发现还是悲剧,晚上参考了两个题解 http://blog.csdn.