A. Island Puzzle
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.

Examples
input

Copy
3
1 0 2
2 0 1
output

Copy
YES
input

Copy
2
1 0
0 1
output

Copy
YES
input

Copy
4
1 2 3 0
0 3 2 1
output

Copy
NO
Note

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的更多相关文章

  1. codeforce B Island Puzzle

    B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. codeforces A. Orchestra B. Island Puzzle

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. codeforces B. Island Puzzle

    B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. CF 936C Lock Puzzle——构造

    题目:http://codeforces.com/contest/936/problem/C 玩了一个小时,只能想出 5*n 的方法. 经过一番观察?考虑这样构造:已经使得 A 串的一个后缀 = B ...

  5. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

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

  7. 冬训 day2

    模拟枚举... A - New Year and Buggy Bot(http://codeforces.com/problemset/problem/908/B) 暴力枚举即可,但是直接手动暴力会非 ...

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

  9. CF#301 D:Bad Luck Island (概率dp)

    D:Bad Luck Island 一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j] ...

随机推荐

  1. oracle 存储过程调用方式

    Oracle存储过程包含三部分:过程声明,执行过程部分,存储过程异常. Oracle存储过程可以有无参数存储过程和带参数存储过程. 一.无参程序过程语法 1 create or replace pro ...

  2. 【搜索】传感器 @upcexam6023

    时间限制: 1 Sec 内存限制: 128 MB 题目描述 SR最近新买了一款电子桌游 这个玩具内部是M个围成一圈的传感器.每个传感器都有开和关两种工作状态,分别用1和0表示.显然,从不同的位置触发沿 ...

  3. FIlter(2)—案例

    案例 demo2,login.jsp请求提交到hello.jsp,该页面中有两个text,分别使用两个Filter链拦截,验证账号密码是否正确,把账号密码设置到Filter初始化参数中 login.j ...

  4. 用R语言分析与预測员工离职

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/kMD8d5R/article/details/83542978 https://mmbiz.qpic ...

  5. apache配置伪静态Rewrite

    1: 修改apache的httpd.conf文件 找到这一行 #LoadModule rewrite_module modules/mod_rewrite.so 改成 LoadModule rewri ...

  6. Jupyter notebook安装扩展插件

    1. 安装Jupyter Notebook pip install jupyter 2. 安装Jypyter Notebook扩展包 pip install jupyter_contrib_nbext ...

  7. CentOS7中设置Tomcat8开机自启动

    CentOS7中设置Tomcat8开机自启动 本文介绍了在centos7中配置tomcat的开机自启动的一些操作步骤,仅供参考. 环境是CentOS 7 ,jdk版本是1.8.0_191,tomcat ...

  8. 【PMP】事业环境因素和组织过程资产

    事业环境因素(EEFs) 事业环境因素(EEFs):是指组织不能控制的,将对项目产生影响.限制或指令作用的各种条件. ①组织内部的事业环境因素: 组织文化.结构和治理 设施和资源的地理分布 基础设施 ...

  9. Pinterest凭什么拥有那么多用户:机器学习是答案

    目前,Pinterest月平均活跃用户量达到1亿,这家以图片为主的公司是如何留住用户并盈利的呢?Pinterest的主要目标是向用户推荐相关的图片或内容,推荐的内容足够精确才能提高用户黏性.近期,&l ...

  10. 开源一个CSV解析器(附设计过程 )

    在ExcelReport支持csv的开发过程中,需要一个NETStandard的csv解析器.在nuget上找了几个试用,但都不太适合. 于是,便有了:AxinLib.IO.CSV. 先看看怎么用: ...