E. Magic Stones CF 思维题
1 second
256 megabytes
standard input
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 ?
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.
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
4
7 2 4 12
7 15 10 12
Yes
3
4 4 4
1 2 3
No
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;
}
E. Magic Stones CF 思维题的更多相关文章
- CF1110E Magic Stones(构造题)
这场CF怎么这么多构造题…… 题目链接:CF原网 洛谷 题目大意:给定两个长度为 $n$ 的序列 $c$ 和 $t$.每次我们可以对 $c_i(2\le i<n)$ 进行一次操作,也就是把 $c ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- Magic Stones CodeForces - 1110E (思维+差分)
E. Magic Stones time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 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] ...
- [Hdu-5155] Harry And Magic Box[思维题+容斥,计数Dp]
Online Judge:Hdu5155 Label:思维题+容斥,计数Dp 题面: 题目描述 给定一个大小为\(N*M\)的神奇盒子,里面每行每列都至少有一个钻石,问可行的排列方案数.由于答案较大, ...
- CF思维联系–CodeForces-217C C. Formurosa(这题鸽了)
ACM思维题训练集合 The Bytelandian Institute for Biological Research (BIBR) is investigating the properties ...
- Codeforces 878D - Magic Breeding(bitset,思维题)
题面传送门 很容易发现一件事情,那就是数组的每一位都是独立的,但由于这题数组长度 \(n\) 很大,我们不能每次修改都枚举每一位更新其对答案的贡献,这样复杂度必炸无疑.但是这题有个显然的突破口,那就是 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
随机推荐
- JQuery Dom的基本操作
这是赋值 $("#test").val ("aaa"); 这是取值 string s = $("#test").val (); text( ...
- Office 365平台及其价值主张
重要提示:<Office 365开发入门指南>视频教程还没有上架,目前会先公开几个小节的文字版本,让大家先睹为快,请大家转发给感兴趣的朋友,敬请留意课程的正式上架和优惠券发放通知. 从今天 ...
- [MySQL] INFORMATION_SCHEMA 数据库包含所有表的字段
sql注入后可以通过该数据库获取所有表的字段信息 1. COLLATIONS表 提供有关每个字符集的排序规则的信息. COLLATIONS表包含以下列: COLLATION_NAME 排序规则名称. ...
- [PHP]算法-队列结构的PHP实现
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路: 1.php数组完全就能实现 2.array_push 从尾部往里压入元素 3.array_shi ...
- Dynamics 365 Online-Virtual Entities
转载来源https://blogs.technet.microsoft.com/lystavlen/2017/09/08/virtual-entities/,使用当前Dynamics 365环境,亲测 ...
- JavaScript面向对象编程指南(五) 原型
第5章 原型 5.1 原型属性 function f(a,b){ return a*b; }; // length 属性 f.length; // constructor 构造属性 f.constru ...
- Glide图片加载框架小bug
如上一段加载图片的代码,本身是没问题的,后来测试发现有情况不显示url对应的图片,而一直显示加载超时的图片 修改如下: 将with()方法的上下文context改为图片的imageView.getCo ...
- Scrollview嵌套Recyclerview嵌套滑动冲突,导致滑动时会出现卡顿的现象
recyclerView.setLayoutManager(new GridLayoutManager(mContext,2){ @Override public boolean canScrollV ...
- [Java]Socket和ServerSocket学习笔记
对于即时类应用或者即时类的游戏,HTTP协议很多时候无法满足于我们的需求.这会,Socket对于我们来说就非常实用了.下面是本次学习的笔记.主要分异常类型.交互原理.Socket.ServerSock ...
- linux tmp清理这些事
tmp目录 首先看下FHS的定义. FHS(Filessystem Hierarchy Standard) 的重点在于规范每个特定的目录下应该要放置什么样子的数据. tmp约定的存放内容 /tmp 这 ...