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. c#类库和可移值类库的区别

    所谓类库,只能指定一个类库的可运行平台. 而可移值类库,可以在无需修改代码的情况,同时可以在多平台上运行DLL文件.多平台如NET Framework.Silverlight.Windows Phon ...

  2. TF2ZP函数

    TF2ZP 中TF是什么意思?   Transfer function   tf 就是传递函数的意思,简称传函 tf2zp是将传递函数转换为零极点形式的一个转换函数   [Z,P,K] = TF2ZP ...

  3. social emotion computing-感情的分类

    第八节  情感的分类 人的情感复杂多样,可以从不同的观察角度进行分类.由于情感的核心内容是价值,因此人的情感主要必须根据它所反映的价值关系的运动与变化的不同特点来进行分类. 1.根据价值的正负变化方向 ...

  4. NumPy 上手一个例子 vectorsum.py

    NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩 ...

  5. 1018Mysql分表分库

    单库单表 单库单表是最常见的数据库设计,例如,有一张用户(user)表放在数据库db中,所有的用户都可以在db库中的user表中查到. 单库多表 随着用户数量的增加,user表的数据量会越来越大,当数 ...

  6. MyBatis学习--简单的增删改查

    jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: Public static void main(String[] args) ...

  7. Entity Framework Code First (一)Conventions

    Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...

  8. Html-Css标签lable中定义宽度需要其他的支持

    lable的标签如果定义了width,如果要使起生效,则需要定义display width: 130px; display: inline-block;

  9. Maven-搭建普通maven项目

    点击Eclipse菜单栏File->New->Ohter->Maven得到如下图所示对话框: 选中Maven Project并点击Next,到下一个对话框(默认)继续点击Next得到 ...

  10. MAC下Eclipse的常用快捷键

    整理Eclipse常用快捷键 开发环境切换到Mac下后原来Window下的快捷键很大一部分是不相容的,习惯了快捷键的生活忽然哪天快捷键不起作用了,跟着的就是开发效率明显降低,频繁录入错误的快捷键让Ec ...