zoukankan      html  css  js  c++  java
  • CF 634A Island Puzzle

    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;
    } 

     

  • 相关阅读:
    DevEco Studio常用设置
    idea报错:Failed to execute goal org.codehaus.mojo:execmavenplugin:3.0.0:exec (defaultcli) on
    HarmonyOS实战 — 第一个入门 HelloWorld
    RabbitMQ入门之HelloWorld
    鸿蒙系统简介
    阿里云短信验证出现签名不合法(不存在或被拉黑)
    鸿蒙和安卓的对比
    关于 SVN 没有加锁 却在 update 和 commit 时 提示 有锁
    ibatis xml 数据库 批量新增 与批量删除
    看不懂的类型转换(有待解决)
  • 原文地址:https://www.cnblogs.com/shenben/p/10494530.html
Copyright © 2011-2022 走看看