CF 634A Island Puzzle
2 seconds
256 megabytes
standard input
standard output
A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
Determine if it is possible for the islanders to arrange the statues in the desired order.
The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands.
The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently placed on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 ≤ bi ≤ n - 1) — the desired statues of the ith island. Once again, bi = 0indicates the island desires no statue. It is guaranteed that the bi are distinct.
Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.
- 3
- 1 0 2
- 2 0 1
- YES
- 2
- 1 0
- 0 1
- YES
- 4
- 1 2 3 0
- 0 3 2 1
- NO
In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2.
In the third sample, no sequence of movements results in the desired position.
【题意】
给定一个字符串你,其中为0的字符可以和相邻的交换位置(环形),问可以不可以变换成给定的字符串
【分析】
显然的交换并不会影响字符串的相对位置,所以记录一下相对位置就可以啦
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,cn1,cn2,a[N],b[N];
inline void Init(){
scanf("%d",&n);
for(int i=1,x;i<=n;i++){scanf("%d",&x);if(x) a[cn1++]=x;}
for(int i=1,x;i<=n;i++){scanf("%d",&x);if(x) b[cn2++]=x;}
}
inline void Solve(){
int pos=-1;
for(int i=0;i<n;i++){
if(b[i]==a[0]){
pos=i;break;
}
}
for(int i=0;i<n-1;i++){
if(a[i]!=b[(i+pos)%(n-1)]){
puts("NO");
return ;
}
}
puts("YES");
}
int main(){
Init();
Solve();
return 0;
}
CF 634A Island Puzzle的更多相关文章
- codeforce B Island Puzzle
B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- codeforces A. Orchestra B. Island Puzzle
A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- codeforces B. Island Puzzle
B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF 936C Lock Puzzle——构造
题目:http://codeforces.com/contest/936/problem/C 玩了一个小时,只能想出 5*n 的方法. 经过一番观察?考虑这样构造:已经使得 A 串的一个后缀 = B ...
- 贪心/构造/DP 杂题选做Ⅱ
由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)
暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...
- 冬训 day2
模拟枚举... A - New Year and Buggy Bot(http://codeforces.com/problemset/problem/908/B) 暴力枚举即可,但是直接手动暴力会非 ...
- cf.301.D. Bad Luck Island(dp + probabilities)
D. Bad Luck Island time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF#301 D:Bad Luck Island (概率dp)
D:Bad Luck Island 一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j] ...
随机推荐
- 【倍增】T-shirt @2018acm徐州邀请赛 I
问题 I: T-shirt 时间限制: 1 Sec 内存限制: 64 MB 题目描述 JSZKC is going to spend his vacation! His vacation has N ...
- performSelector may cause a leak because its selector is unknown
转自:http://www.jianshu.com/p/6517ab655be7 问题 我在 ARC 模式下编译出了这个 warning: "performSelector may caus ...
- ASP.NET Core使用Razor页面
ASP.NET Core使用Razor页面 Razor是ASP.NET的页面引擎,在ASP.NET MVC 3以后被广泛使用,我在之前的博客中有所介绍,需要更多了解的朋友请移步[Razor语法] 在A ...
- 在Mac平台上安装配置ELK时的一些总结
一.前言 大数据处理是流行的一些表现,在不断壮大的数据处理中,怎么样处理数据才是我们继续做好开发的正道.本文章来自网络,不敢原创,但是也有很大借鉴. 二.MAC安装ELK 首先是安装elast ...
- Java程序猿怎样高速理解Kubernetes
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/82892167 https: ...
- PL/SQL学习笔记之事务
一:事务自动提交的开启与关闭 1)开启事务自动提交:则每一个INSERT,UPDATE或DELETE命令执行时,都提交一次事务. SET AUTOCOMMIT ON; 2)关闭事务自动提交:则执行到C ...
- assert BOOST_ASSERT的坑
下面这行代码 BOOST_ASSERT(SUCCEEDED(m_pd3dDevice->CreateBuffer(&frame_ptr->m_const_buffers[i].m_ ...
- 通过__block的作用深入研究block
block普通引用 默认情况下,在block中访问外部变量是通过复制一个变量来操作的,既可以读,但是写操作不对原变量生效,下面通过代码来举证 NSString *a = @"testa&qu ...
- 中期linux课程考试题
[口头表达] 1)请描述你了解的磁盘分区的相关知识2)什么是rsync,你有什么生产环境的应用?3)在生产环境中,公司的IDC机房即将超过254台机器,请问你有什么解决方案来规划扩展IDC机房的内网网 ...
- BizTalk RosettaNet 开发笔记
RNIF BAM Tracking Error  解决办法:  503: Service Unavailable   解决办法:IIS 应用程序池运行账户用户名或密码错误,用户名不能是doma ...