Codeforces 635B Island Puzzle【最小表示法+思维】
来源:互联网 发布:dnf经常网络中断2017 编辑:程序博客网 时间:2024/04/29 08:17
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.
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.
Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.
31 0 22 0 1
YES
21 00 1
YES
41 2 3 00 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.
题目大意:
现在有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"); }}
- Codeforces 635B Island Puzzle【最小表示法+思维】
- CodeForces 635B Island Puzzle(小岛放雕塑,思维)
- CodeForces 635B Island Puzzle
- codeforces A. Orchestra B. Island Puzzle
- Codeforences B. Island Puzzle
- Codeforces 496B Secret Combination(最小表示法)
- codeforces 559B Equivalent Strings (最小表示法)
- Codeforces 634A Island Puzzle 【水题】
- CodeForces 634A-Island Puzzle【找规律】
- CodeForces 634 A.Island Puzzle(水~)
- codeforces(559B)--B. Equivalent Strings(暴搜 或 最小表示法)
- Codeforces Round #394 (Div. 2) B. Dasha and friends —— 暴力 or 最小表示法
- Codeforces Round #385 (Div. 2)B. Hongcow Solves A Puzzle【思维+暴力】
- Codeforces Round #385 (Div. 2) B. Hongcow Solves A Puzzle 几何、思维题
- [思维]CodeForces 135B
- 【codeforces B + 思维】
- codeforces-233【B思维】
- CodeForces 219B (思维)
- url 编码(percentcode 百分号编码)
- [译] 关于 Git 你需要知道的一些事情
- 错误宏定义ERR_EXIT
- spring boot整合redis实现缓存机制
- Spring 定时任务之 @Scheduled cron表达式
- Codeforces 635B Island Puzzle【最小表示法+思维】
- 第二篇、创建型设计模式——工厂方法模式
- iOS NSPredicate特殊字符判断无法正常使用
- JS中使用Date对象实时显示系统时间小示例
- Android 资源下载地址
- Hi3519V100与Hi3519V101的区别
- clock_gettime接口和linux时间系统
- BZOJ 1007
- No service of type Factory available in ProjectScopeServices apply plugin: 'com.github.dcendents.an