[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)=c_i-c_{i-1}$,也就是说交换了原差分数组的两位。

所以就把$c$数组差分一下,看是不是和$t$数组相同即可,注意判断$c_1,c_n$是否和$t_1,t_n$相同,因为这两个位置无法做变换。

卡点:

C++ Code:

#include <algorithm>
#include <cstdio>
#define maxn 100010
int n;
int s[maxn], t[maxn];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) scanf("%d", s + i);
	for (int i = 1; i <= n; ++i) scanf("%d", t + i);
	if (s[1] != t[1] || s[n] != t[n]) {
		puts("No");
		return 0;
	}
	for (int i = n; i + 1; --i) {
		s[i] -= s[i - 1];
		t[i] -= t[i - 1];
	}
	std::sort(s + 2, s + n + 1); std::sort(t + 2, t + n + 1);
	for (int i = 2; i <= n; ++i) if (s[i] != t[i]) {
		puts("No");
		return 0;
	}
	puts("Yes");
	return 0;
}

  

原文地址:https://www.cnblogs.com/Memory-of-winter/p/10359547.html

时间: 2024-11-13 02:13:56

[CF1110E]Magic Stones的相关文章

【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

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

【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(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

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\) ,则

(状压dp)HDU 4778 Gems Fight!

Gems Fight! Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)Total Submission(s): 2395    Accepted Submission(s): 1087 Problem Description Alice and Bob are playing "Gems Fight!": There are Gems of G differe