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]不能变,所以要判断c[1]和t[1],c[n]和t[n]是否相等
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#define maxn 100005
using namespace std;
int n;
int a[maxn];
int b[maxn];
int da[maxn];
int db[maxn];
int is_eqal(int *a,int *b,int sz){
sort(a+1,a+1+sz);
sort(b+1,b+1+sz);
for(int i=1;i<=sz;i++){
if(a[i]!=b[i]) return 0;
}
return 1;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
for(int i=1;i<n;i++){
da[i]=a[i+1]-a[i];
}
for(int i=1;i<n;i++){
db[i]=b[i+1]-b[i];
}
if(a[1]!=b[1]||a[n]!=b[n]){
printf("No\n");
}else{
if(is_eqal(da,db,n-1)) printf("Yes\n");
else printf("No\n");
}
}
Codeforces 1110E (差分)的更多相关文章
- Greg and Array CodeForces 296C 差分数组
Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...
- Magic Stones CodeForces - 1110E (思维+差分)
E. Magic Stones time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces.1110E.Magic Stones(思路 差分)
题目链接 听dalao说很nb,做做看(然而不小心知道题解了). \(Description\) 给定长为\(n\)的序列\(A_i\)和\(B_i\).你可以进行任意多次操作,每次操作任选一个\(i ...
- 【Codeforces 1110E】Magic Stones
Codeforces 1110 E 题意:给定两个数组,从第一个数组开始,每次可以挑选一个数,把它变化成左右两数之和减去原来的数,问是否可以将第一个数组转化成第二个. 思路: 结论:两个数组可以互相转 ...
- Codeforces 1119D(差分)
题面 传送门 分析 先考虑\(O(nk)\)的做法,先按s从小到大排序,每个串的数显然形成了n个连续区间\([s_i+l,s_i+r]\),且这些区间的左端点升序排列,然后把区间合并就可以知道有多少个 ...
- G - Greg and Array CodeForces - 296C 差分+线段树
题目大意:输入n,m,k.n个数,m个区间更新标记为1~m.n次操作,每次操作有两个数x,y表示执行第x~y个区间更新. 题解:通过差分来表示某个区间更新操作执行的次数.然后用线段树来更新区间. #i ...
- Codeforces Gym 100015G Guessing Game 差分约束
Guessing Game 题目连接: http://codeforces.com/gym/100015/attachments Description Jaehyun has two lists o ...
- codeforces 1017C - Cloud Computing 权值线段树 差分 贪心
https://codeforces.com/problemset/problem/1070/C 题意: 有很多活动,每个活动可以在天数为$[l,r]$时,提供$C$个价格为$P$的商品 现在从第一天 ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
随机推荐
- How to compile and install Linux Kernel 5.1.2 from source code
How to compile and install Linux Kernel 5.1.2 from source code Compiling a custom kernel has its adv ...
- oracle汇编03
.long expression1, expression2, ..., expressionNThe .long directive generates a long integer (32-bit ...
- console.log 不起作用
devtool console.log 突然不起作用了
- php import require include use vendor
一.use 调用命名空间 用法. use app\common\controller\Index as commonIndex 或 use app\common\controller\Index ...
- 使用rabbitctl添加用户
使用rabbitctl添加用户 第一.添加mq用户并设置密码 root@live-mq-01:~ # rabbitmqctl add_user mq 123456 1 root@live-mq-01: ...
- MySQL数据库5事务、视图、触发器、函数、数据库的备份
目录 一.事务(important) 1.1什么是事务? 1.2解决办法 1.2.1事务的语法 1.2.2使用事务解决转账问题代码演示 1.2.3rollback 1.3事务的特性(important ...
- 【leetcode】1030. Matrix Cells in Distance Order
题目如下: We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), whe ...
- animation transition transform
animation:动画名称 花费时间 运动曲线 何时开始 播放次数 是否反方向 div{width:100px;height:100px;background:red;animation:move ...
- common pom
<dependencies> <dependency> <groupId>com.github.pagehelper</groupId> <art ...
- RSS(简易信息聚合)和jieba(第三方分词组件)
简易信息聚合(也叫聚合内容)是一种RSS基于XML标准,在互联网上被广泛采用的内容包装和投递协议.RSS(Really Simple Syndication)是一种描述和同步网站的内容格式,是使用最广 ...