Codeforces 635B Island Puzzle【最小表示法+思维】

来源:互联网 发布:dnf经常网络中断2017 编辑:程序博客网 时间:2024/04/29 08:17

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, labeled1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islandsn 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 integersai (0 ≤ ai ≤ n - 1) — the statue currently placed on thei-th island. If ai = 0, then the island has no statue. It is guaranteed that theai are distinct.

The third line contains n space-separated integersbi (0 ≤ bi ≤ n - 1) — the desired statues of theith island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that thebi are distinct.

Output

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

Examples
Input
31 0 22 0 1
Output
YES
Input
21 00 1
Output
YES
Input
41 2 3 00 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个岛屿形成一个环状,1和2相邻,2和3相邻................n和1相邻。

此时门有N-1个编号的物体在各个岛屿上,有一个岛屿是空的。

一个岛屿不能有两个物品同时存在。

我们每次可以将一个物品搬到相邻的空岛屿上。

给出一个目标情况,问能否通过这样的挪动方式得到。


思路:


1、对于空岛屿来讲,就是一个媒介,可以使得除了这个空岛屿之外的所有岛屿可以进行顺时针或者是逆时针的旋转,而并不能改变相对位子。


2、所以我们将空岛屿去除掉之后,只要求出两个序列的最小表示法,并且进行比对,如果两个序列的最小表示法相等,那么就是YES的情况,否则就是NO的情况。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;int a[205000];int b[205000];int c[205000];int d[205000];int n;int MinimumRepresentation(int b[]){    int l=n-1;    int i = 0, j = 1, k = 0, t;    while(i < l && j < l && k < l) {        t = b[(i + k) >= l ? i + k - l : i + k] - b[(j + k) >= l ? j + k - l : j + k];        if(!t) k++;        else{            if(t > 0) i = i + k + 1;            else j = j + k + 1;            if(i == j) ++ j;            k = 0;        }    }    return (i < j ? i : j);}int main(){    while(~scanf("%d",&n))    {        int cnt=0;        for(int i=0;i<n;i++)        {            int x;            scanf("%d",&x);            if(x==0)continue;            a[cnt++]=x;        }        int tmp=MinimumRepresentation(a);        for(int i=0;i<n-1;i++)b[i]=a[(i+tmp)%(n-1)];        cnt=0;        for(int i=0;i<n;i++)        {            int x;            scanf("%d",&x);            if(x==0)continue;            c[cnt++]=x;        }        tmp=MinimumRepresentation(c);        for(int i=0;i<n-1;i++)d[i]=c[(i+tmp)%(n-1)];        int flag=0;        for(int i=0;i<n-1;i++)        {            if(b[i]!=d[i])flag=1;        }        if(flag==0)printf("YES\n");        else printf("NO\n");    }}






0 0
原创粉丝点击