题面

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\)), 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 \(c_i\) changes to \(c_i' = c_{i + 1} + c_{i - 1} - c_i\)

Andrew, Grigory's friend, also has n n stones with charges \(t_i\). 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 \(c_i\) into \(t_i\) for all \(i\)?

题意

给定两个数组 \(c\) 和 \(t\),一次操作可以把 \(c_i\) 变成 \(c_{i-1}+c_{i+1}-c_{i} \ (2 \le i \le n-1)\),问若干次操作后,可不可以把 \(c\) 数组变成 \(t\)

思路

设 \(a\) 为 \(c\) 的差分数组(即 \(a_i=c_i-c_{i-1}\))

每对 \(c_i\) 进行一次操作后:

\[a_i=c_i-c_{i-1}=(c_{i+1}-c_{i-1}-c_i)-c_{i-1}=c_{i+1}-c_i=a_{i+1}
\]

\[a_{i+1}=c_{i+1}-c{i}=c_{i+1}-(c_{i+1}-c_{i-1}-c_i)=c_i-c_{i-1}=a_{i}
\]

所以,每次操作会将 \(c\) 的差分数组交换两个数,只要判一下 \(c\) 与 \(t\) 的差分数组是否相同即可

代码

#include <bits/stdc++.h>
using namespace std;
int n,a[100001],b[100001],c[100001],d[100001];
int main() {
cin >> n;
for (int i = 1;i <= n;i++) {
cin >> a[i];
if (i ^ 1) b[i-1] = a[i]-a[i-1];
}
sort(b+1,b+n);
for (int i = 1;i <= n;i++) {
scanf("%d",&c[i]);
if (i ^ 1) d[i-1] = c[i]-c[i-1];
}
sort(d+1,d+n);
if (a[1] ^ c[1] || a[n] ^ c[n]) {
printf("No");
return 0;
}
for (int i = 1;i < n;i++)
if (b[i] ^ d[i]) {
printf("No");
return 0;
}
printf("Yes");
return 0;
}

【CF1110E】 Magic Stones - 差分的更多相关文章

  1. CF1110E Magic Stones 差分

    传送门 将原数组差分一下,设\(d_i = c_{i+1} - c_i\) 考虑在\(i\)位置的一次操作会如何影响差分数组 \(d_{i+1}' = c_{i+1} - (c_{i+1} + c_{ ...

  2. CF1110E Magic Stones(构造题)

    这场CF怎么这么多构造题…… 题目链接:CF原网 洛谷 题目大意:给定两个长度为 $n$ 的序列 $c$ 和 $t$.每次我们可以对 $c_i(2\le i<n)$ 进行一次操作,也就是把 $c ...

  3. [CF1110E]Magic Stones

    题目大意:有一个长度为$n(n\leqslant10^5)$的数列$c$,问是否可以经过若干次变换变成数列$t$,一次变换为$c'_i=c_{i+1}+c_{i-1}-c_i$ 题解:思考一次变换的本 ...

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

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

  5. E. Magic Stones CF 思维题

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

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

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

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

  8. 【Codeforces 1110E】Magic Stones

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

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

    题意 给定两个数组c和t,可以对c数组中的任何元素变换\(c_i\)​成\(c_{i+1}+c_{i-1}-c_i\)​,问c数组在若干次变换后能否变换成t数组. 分析 这种魔法题目我是同样的没做过. ...

随机推荐

  1. 食用Win系统自带的PowerShell登录服务器

    运行powershell输入ssh root@你的服务器ip -p你的端口 切换rm ~/.ssh/known_hosts cmd 运行 ping 你的ip -t一直ping ctrl+c停止 tra ...

  2. Makefile中的一个坑

    问题描述:Makefile中,我想将一个变量的后缀全部进行替换,如将所有的.c后缀变成.d后缀 方法:$(CUR_SOURCE: .c = .d ) 说明:查阅相关资料,了解到上述这种语法就可以将所有 ...

  3. finalize()和四种引用的一点思考

    一次对ThreadLocal的学习引发的思考 ThreadLocal对Entry的引用是弱引用,于是联想到四种引用的生命周期. 强引用,不会进行垃圾回收 软引用,JVM内存不够,进行回收 弱引用,下次 ...

  4. JS 原生ajax写法

    <script> //step1.创建XMLHTTPRequest对象,对于低版本的IE,需要换一个ActiveXObject对象 var xhr; if (window.XMLHttpR ...

  5. java JDBC自我总结

    preparedstatement和statement的区别 当不需要预编译时(不需要占位符)可以选用statement,存在不安全 当有占位符(?)时,需要选用preparedstatement s ...

  6. 《闲扯Redis八》Redis字典的哈希表执行Rehash过程分析

    一.前言 随着操作的不断执行, 哈希表保存的键值对会逐渐地增多或者减少, 为了让哈希表的负载因子(load factor)维持在一个合理的范围之内, 当哈希表保存的键值对数量太多或者太少时, 程序需要 ...

  7. Python while 中简单的语句组

    Python while 中简单的语句组: 只使用 while: # 简单的语句组 a = 4 b = 8 num = 0 while a < b: print("a 比 b 小&qu ...

  8. 最基本的Tkinter界面操作

    1.创建应用程序主窗口对象 root = Tk() 2.在主窗口中,添加各种可视化组件 btn1 = Button(root) btn1["text"] = "点我&qu ...

  9. Python File fileno() 方法

    概述 fileno() 方法返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作.高佣联盟 www.cgewang.com 语法 fileno() ...

  10. PHP date_interval_format() 函数

    ------------恢复内容开始------------ 计算两个日期间的间隔,然后格式化时间间隔: 实例 <?php $date1=date_create("2013-01-01 ...