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

     

  • 相关阅读:
    【腾讯Bugly干货分享】微信Tinker的一切都在这里,包括源码(一)
    【腾讯Bugly干货分享】iOS10 SiriKit QQ适配详解
    【腾讯Bugly干货分享】安卓单元测试:What, Why and How
    【腾讯Bugly干货分享】Android Linker 与 SO 加壳技术
    【腾讯优测干货分享】Android内存泄漏的简单检查与分析方法
    【腾讯Bugly经验分享】程序员的成长离不开哪些软技能?
    【腾讯Bugly干货分享】基于 Webpack & Vue & Vue-Router 的 SPA 初体验
    从零开始安装Hadoop视频教程
    如何在MAC机器中实现移动设备WiFI上网(没有专门的无线路由器的情况)
    Alfresco安装与配置图解
  • 原文地址:https://www.cnblogs.com/shenben/p/10494530.html
Copyright © 2011-2022 走看看