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=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int b[maxn];
int main()
{
gbtb;
cin>>n;
repd(i,,n)
{
cin>>a[i];
}
repd(i,,n)
{
cin>>b[i];
} std::vector<int> v1;
std::vector<int> v2;
bool isok=;
if(a[]!=b[]||a[n]!=b[n])
{
// db(2);
isok=;
} repd(i,,n)
{
v1.pb(a[i]-a[i-]);
v2.pb(b[i]-b[i-]);
}
int z=sz(v1);
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
repd(i,,z-)
{
if(v1[i]!=v2[i])
{
isok=;
}
}
if(isok)
{
printf("Yes\n");
}else
{
printf("No\n");
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

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

  1. Codeforces 1110E (差分)

    题面 传送门 分析 一开始考虑贪心和DP,发现不行 考虑差分: 设d[i]=c[i+1]-c[i] (i<n) 那么一次操作会如何影响差分数组呢? \(c[i]'=c[i+1]+c[i-1]-c ...

  2. E. Magic Stones CF 思维题

    E. Magic Stones time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. 【CF1110E】 Magic Stones - 差分

    题面 Grigory has n n magic stones, conveniently numbered from \(1\) to \(n\). The charge of the \(i\)- ...

  4. 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] ...

  5. Magic Numbers CodeForces - 628D

    Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...

  6. Dima and Magic Guitar CodeForces - 366E

    Dima and Magic Guitar CodeForces - 366E 题意: http://blog.csdn.net/u011026968/article/details/38716425 ...

  7. LZH的多重影分身 qduoj 思维 差分

    LZH的多重影分身 qduoj 思维 差分 原题链接:https://qduoj.com/problem/591 题意 在数轴上有\(n\)个点(可以重合)和\(m\)条线段(可以重叠),你可以同时平 ...

  8. Codeforces.1110E.Magic Stones(思路 差分)

    题目链接 听dalao说很nb,做做看(然而不小心知道题解了). \(Description\) 给定长为\(n\)的序列\(A_i\)和\(B_i\).你可以进行任意多次操作,每次操作任选一个\(i ...

  9. 【Codeforces 1110E】Magic Stones

    Codeforces 1110 E 题意:给定两个数组,从第一个数组开始,每次可以挑选一个数,把它变化成左右两数之和减去原来的数,问是否可以将第一个数组转化成第二个. 思路: 结论:两个数组可以互相转 ...

随机推荐

  1. malloc和calloc用法

    malloc和calloc用法 #include <stdio.h> #include <stdlib.h> int main(){ int n; printf("i ...

  2. Lua代码规范

    以下规范,是在Unity中使用Lua做为开发语言,仅供参考. 1.格式规范 1. lua文件名统一小写,中间一律不加下划线分割 2. 类名首字母大写,多个词组成的类名,每个词的首字母大写,中间一律不加 ...

  3. phprpc的使用示例以及报错Fatal error: Cannot redeclare gzdecode() in D:\wamp\www\immoc\phprpc\compat.php 处理

    今天看书,发现了PHPRPC这个好东东,故在此写下来以作笔记. PHPRPC 是一个轻型的.安全的.跨网际的.跨语言的.跨平台的.跨环境的.跨域的.支持复杂对象传输的.支持引用参数传递的.支持内容输出 ...

  4. JavaScript -- 时光流逝(五):js中的 Date 对象的方法

    JavaScript -- 知识点回顾篇(五):js中的 Date 对象的方法 Date 对象: 用于处理日期和时间. 1. Date对象的方法 <script type="text/ ...

  5. C# -- 使用System.Environment获取电脑的相关属性

    使用System.Environment获取电脑的相关属性 1.使用System.Environment获取电脑的相关属性(入门案例) static void Main(string[] args) ...

  6. 《数据库技术基础与应用(第2版)》学习笔记——第7章~

    从这章开始,操作的内容开始增多,概念的东西越来越少,可能跟学校的教学目的有关,但是跟我的学习目的不匹配,就不再继续整理. 总结:这本书适合大学本科生学习和了解数据库的相关知识以及Access和SQL ...

  7. Nginx 的 TCP 负载均衡介绍

    Nginx除了以前常用的HTTP负载均衡外,Nginx增加基于TCP协议实现的负载均衡方法. HTTP负载均衡,也就是我们通常所有“七层负载均衡”,工作在第七层“应用层”.而TCP负载均衡,就是我们通 ...

  8. ubuntu下配置rsync,实现远程备份

    rysnc(remote synchronize)在CentOS系统默认安装在/usr/bin,此外rysnc在windows平台下也有相应版本.主页地址为: http://rsync.samba.o ...

  9. router-link 自定义点击事件

    <li v-for="(item, index) in menuList"> <router-link class="classify" ta ...

  10. zabbix全网监控

    为什么要监控 运维的职责1.保障企业数据的安全可靠.2.为客户提供7*24小时服务.3.不断提升用户的体验. 在关键时刻,提前提醒我们服务器要出问题了 当出问题之后,可以便于找到问题的根源 拿到公司服 ...