Codeforces1110E Magic Stones

本文解析了CodeForces竞赛中一道关于数组操作的问题,详细介绍了如何通过构建差分数组来判断两个序列是否可通过特定操作相互转换。核心在于理解差分数组的特性,通过排序比较实现高效解法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://codeforces.com/contest/1110/problem/E

Grigory has nn magic stones, conveniently numbered from 11 to nn . The charge of the ii -th stone is equal to cici .

Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index ii , where 2≤i≤n−12≤i≤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 cici changes to c′i=ci+1+ci−1−cici′=ci+1+ci−1−ci .

Andrew, Grigory's friend, also has nn stones with charges titi . 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 cici into titi for all ii ?

Input

The first line contains one integer nn (2≤n≤1052≤n≤105 ) — the number of magic stones.

The second line contains integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤2⋅1090≤ci≤2⋅109 ) — the charges of Grigory's stones.

The second line contains integers t1,t2,…,tnt1,t2,…,tn (0≤ti≤2⋅1090≤ti≤2⋅109 ) — the charges of Andrew's stones.

Output

If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".

Otherwise, print "No".

题意:有一序列c(i),和t(i),问能否通过若干次操作将c变为t。每次操作是:选择一个i∈(1,n),改变c(i)为c(i-1)+c(i+1)-c(i)。

做法:建立差分数组d,d(i)=c(i+1)-c(i),然后对c(i)做一次操作d会发生如下变化:

即c(i)一次操作等价于交换d(i-1),d(i)位置。

那么c能转化为t等价于c的差分数组元素两两交换能变为t的差分数组  ,排个序就行了

差分数组相同自然原数组也相同

开始时特判一下c的第一个和最后一个元素必须等于t的第一个和最后一个。

#include<bits/stdc++.h>
using namespace std;
#define maxn 100000+100

int n,s[maxn],t[maxn],ds[maxn],dt[maxn];

int main()
{
//	freopen("input.in","r",stdin);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>s[i];
	for(int i=1;i<=n;i++)cin>>t[i];
	if(s[1]!=t[1]||s[n]!=t[n])puts("No");
	else
	{
		for(int i=2;i<=n;i++)ds[i]=s[i]-s[i-1],dt[i]=t[i]-t[i-1];
		sort(ds+2,ds+n+1);
		sort(dt+2,dt+n+1);
		for(int i=2;i<=n;i++)if(ds[i]!=dt[i])
		{
			puts("No");
			exit(0);
		}
		puts("Yes");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值