题面

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. 感知机算法(PLA)代码实现

    目录 1. 引言 2. 载入库和数据处理 3. 感知机的原始形式 4. 感知机的对偶形式 5. 多分类情况-one vs. rest 6. 多分类情况-one vs. one 7. sklearn实现 ...

  2. 二分图&网络流初步

    链接 : 最小割&网络流应用 EK太低级了,不用. 那么请看:#6068. 「2017 山东一轮集训 Day4」棋盘,不用EK你试试? dinic模板及部分变形应用见zzz大佬的博客:网络流学 ...

  3. Oracle对表进行备份

    前言: 在实际开发中,我们常常需要对单张或多张表进行备份,以下博主就从这两个方面进行总结.如需转载,请标明来处,谢谢! 在备份前我们先创建表盒相关测试的数据 -- Create table creat ...

  4. WPF入门教程(一)---基础

    这篇主要讲WPF的开发基础,介绍了如何使用Visual Studio 2013创建一个WPF应用程序. 首先说一下学习WPF的基础知识: 1) 要会一门.NET所支持的编程语言.例如C#. 2) 会一 ...

  5. vue+springboot文件上传

    //vue element-ui组件 <el-upload style="position: relative;top: -40px;left: 240px;"        ...

  6. Centos 7 下安装PHP7.2(与Apache搭配的安装方式)

    (1)源码包下载 百度云下载地址:https://pan.baidu.com/s/1xH7aiGYaX62wij4ul5P-ZQ 提取码:m9zc (2)安装php依赖组件: yum -y insta ...

  7. Bug:No mapping for GET /onepill//swagger-ui.html

    SpringBoot使用Swagger2本来可以使用的,后来出现的异常No mapping for GET /swagger-ui.html,这个异常其实不用怎么解释,说白了就是找不到了. 遇到这种情 ...

  8. 关于android的监听器

    import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bu ...

  9. 在Linux系统中使用Vim读写远程文件

    大家好,我是良许. 今天我们讨论一个 Vim 使用技巧--用 Vim 读写远程文件.要实现这个目的,我们需要使用到一个叫 netrw.vim 的插件.从 Vim 7.x 开始,netrw.vim 就被 ...

  10. PHP array_uintersect_uassoc() 函数

    实例 比较两个数组的键名和键值(使用用户自定义函数进行比较),并返回交集: <?phpfunction myfunction_key($a,$b){if ($a===$b){return 0;} ...