【CF1110E】 Magic Stones - 差分

题面

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;
}

原文地址:https://www.cnblogs.com/lrj124/p/11776712.html

时间: 2024-11-10 18:28:33

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

[CF1110E]Magic Stones

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

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

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 G

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]=b[i-1]+b[i+1]-b[i].问进行一些操作后,a和b能否相同. 分析: 考虑一次操作会变成什么样子. a b c a a+c-b c 我们发现这些数字差分后是不变的.于是对两个数组差分后,排序,看是否一样即可.注意判一下1和n是否相等. 代码: #include<cstdio> #in

E. Magic Stones CF 思维题

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 . Sometime

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

题意 给定两个数组c和t,可以对c数组中的任何元素变换\(c_i\)?成\(c_{i+1}+c_{i-1}-c_i\)?,问c数组在若干次变换后能否变换成t数组. 分析 这种魔法题目我是同样的没做过.Editorial里说用差分来能够看出思路.问题是,如何能够想到差分来做?因为它的变换就是相邻的数的加减法,那么想要找到思路的突破口必须也得从这里突破. 考虑变换前后的数组: 原来:\(a_{i-1}, a_i, a_{i+1}\) 之后:\(a_{i-1}, a_{i-1}+a_{i+1}-a_i

【Codeforces Global Round 1 E】Magic Stones

[链接] 我是链接,点我呀:) [题意] 你可以把c[i]改成c[i+1]+c[i-1]-c[i] (2<=i<=n-1) 问你能不能把每一个c[i]都换成对应的t[i]; [题解] d[i] = c[i+1]-c[i]; (1<=i<=n-1) change c[i] c[i]' = c[i+1]+c[i-1]-c[i]; d[i-1] = c[i]'-c[i-1]; = c[i+1]+c[i-1]-c[i]-c[i-1] == c[i+1]-c[i] = d[i]; d[i]

Magic Stones(思维题)

#include <bits/stdc++.h> using namespace std; typedef long long ll; int v1[100001]; int v2[100001]; int s1[100001]; int s2[100001]; int main () { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> v1[i]

AT2164 AGC006C Rabbit Exercise

AT2164 AGC006C Rabbit Exercise 数轴上有 \(n\) 个点,每个点的坐标为 \(a_i\) .一轮操作包含 \(m\) 次变换,第 \(i\) 次将 \(b_i(1<b_i<n)\) 随机跳到点关于 \(b_i-1\) 或 \(b_i+1\) 的对应点.求 \(k\) 轮操作后每个点的期望位置 \(n,\ m\leq10^5,\ k\leq10^{18},\ |a_i|\leq10^9\) 期望,差分,倍增 假设点 \(a_i\) 变换后坐标为 \(x\) ,则

Codeforces Global Round 1

A. Parity 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 100010 5 int b, k, a[N]; 6 7 int main() 8 { 9 while (scanf("%d%d", &b, &k) != EOF) 10 { 11 int res = 0; 12 for (int i = 1; i <= k; ++i) scanf("%d&