B. 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
3
1 0 2
2 0 1
output
YES
input
2
1 0
0 1
output
YES
input
4
1 2 3 0
0 3 2 1
output
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.

回过头来想想这题其实挺水的,然而我却没做出来

题意:就是n个数编号,并且首尾相连,其中0能和的交换,判断能不能通过交换达到下面的目的序列

分析:其实不管怎么怎么于0交换,他们的顺序是不变的,就是从第一组数0后面的数的顺序跟第二组是一样的

 #include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int Max = ;
int a[Max + ], b[Max + ];
int main()
{
int n, s, t;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
if(a[i] == )
s = i;
}
s = (s + ) % n;
for(int i = ; i < n; i++)
{
scanf("%d", &b[i]);
if(b[i] == a[s])
t = i;
}
int m = n - ;
int flag = false;
while(m--)
{
if(a[s] == )
s = (s + ) % n;
if(b[t] == )
t = (t + ) % n;
if(a[s] == b[t])
{
s = (s + ) % n;
t = (t + ) % n;
continue;
}
else
{
flag = true;
break;
}
}
if(flag)
printf("NO\n");
else
printf("YES\n");
return ;
}

codeforce B Island Puzzle的更多相关文章

  1. codeforces A. Orchestra B. Island Puzzle

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

  2. CF 634A Island Puzzle

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

  3. codeforces B. Island Puzzle

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

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

  5. 冬训 day2

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

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

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

  7. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题

    B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...

  8. 蓝眼睛与红眼睛(The blue-eyed islanders puzzle)

    澳大利亚的华裔数学神童陶哲轩曾在网上贴出来一个问题 The blue-eyed islanders puzzle 让大家思考,逗大家玩儿. 说一个岛上有100个人,其中有5个红眼睛,95个蓝眼睛.这个 ...

  9. D. Bash and a Tough Math Puzzle 解析(線段樹、數論)

    Codeforce 914 D. Bash and a Tough Math Puzzle 解析(線段樹.數論) 今天我們來看看CF914D 題目連結 題目 給你一個長度為\(n\)的數列\(a\), ...

随机推荐

  1. 【BZOJ1002】【FJOI2007】轮状病毒(生成树计数)

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1766  Solved: 946[Submit][Status ...

  2. Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)

    /*****************开启Activity  并传递参数*******************/ 使用am命令启动Activity并传递参数的方法,也能用作C层与Java进行数据传递的一 ...

  3. 【web端】百度地图api

    百度地图js加载 页面引入: 修改申请的密钥:<script type="text/javascript" src="http://api.map.baidu.co ...

  4. 【BZOJ 1857】【SCOI 2010】传送带

    三分套三分,虽然简单,但是也得掌握,,, 时间复杂度$O(log_{1.5}^2 n)$ 一开始WA好几次发现是快速读入里没有return,这样也能过样例?_(:3J∠)_ #include<c ...

  5. 关于php插件pdo_mysql的安装

    今天在做一个商城的连接的时候,需要MpDO验证.需要安装pdo_mysql模块,刚开始按照php扩展模块的安装按照这个安装ZIP,curl都成功了但是安装pdo_mysql却不行,在./configu ...

  6. jquery-通过js编写弹出窗口

    本文转载 本文主要是通过js动态控制div的高度,css控制浮动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...

  7. eclipse-插件安装的三种方式

    (前两种安装方式以多国语言包的安装为例) 1.  普通安装:用直接解压的安装方式来实现 解压插件到某个文件夹 将下载的插件文件解压到 Eclipse 的安装目录下 如插件文件为多国语言包: NLpac ...

  8. C#-WinForm-弹窗提示框-如何知道用户点击的是哪个按钮?

    MessageBox.Show() 有21个重载 常用的弹窗提示框 1.一个参数,弹窗只有一个选项 2.三个参数,第一个参数是设置弹窗消息框中的文字内容:第二个参数是设置弹窗标题栏中显示的文本:第三个 ...

  9. CSS强制英文、中文换行与不换行 强制英文换行

    1. word-break:break-all;只对英文起作用,以字母作为换行依据 2. word-wrap:break-word; 只对英文起作用,以单词作为换行依据 3. white-space: ...

  10. 【2016多校训练4】Multi-University Training Contest 4

    1001  Another Meaning 题意:字符串A中包含的字符串B可以翻译或不翻译,总共有多少方案. 题解:动规,dp[i]表示A的第i位为止有多少方案. 转移方程: dp[i]=dp[i-1 ...