• loj #6091. 「Codeforces Round #418」幻想特快


    #6091. 「Codeforces Round #418」幻想特快

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<queue>
     6 using namespace std;
     7 #define maxn 10000
     8 int n,a[maxn],b[maxn],p[maxn],nxt1,nxt2,tot,x1,x2,y1,y2;
     9 bool vis[maxn];
    10 char ch;
    11 inline void read(int &now)
    12 {
    13     int f=1; ch=getchar(); now=0;
    14     while(ch>'9'||ch<'0') {if(ch=='-') f*=-1; ch=getchar();}
    15     while(ch>='0'&&ch<='9') now=now*10+ch-'0',ch=getchar();
    16     now*=f;
    17 }
    18 
    19 int main()
    20 {
    21     read(n);
    22     for(int i=1;i<=n;i++) read(a[i]);
    23     for(int i=1;i<=n;i++) read(b[i]);
    24     for(int i=1;i<=n;i++)
    25     {
    26         if(a[i]!=b[i])
    27         {
    28             tot++;
    29             if(tot==1) nxt1=i,x1=a[i],x2=b[i];
    30             else nxt2=i,y1=a[i],y2=b[i];
    31         }
    32     }
    33     if(tot==1)
    34     {
    35         for(int i=1;i<=n;i++)
    36             if(i!=nxt1) p[i]=a[i],vis[p[i]]=1;
    37         for(int i=1;i<=n;i++)
    38         {
    39             if(i==nxt1)
    40             {
    41                 for(int j=1;j<=n;j++)
    42                 {
    43                     if(j!=x1&&j!=x2&&!vis[j])
    44                     {
    45                         p[i]=j;
    46                         break;
    47                     }
    48                 }
    49             }
    50         }
    51     }
    52     if(tot==2)
    53     {
    54         for(int i=1;i<=n;i++)
    55             if(i!=nxt1&&i!=nxt2) p[i]=a[i],vis[p[i]]=1;
    56         for(int i=1;i<=n;i++)
    57         {
    58             if(i==nxt1)
    59             {
    60                 if(!vis[a[i]]) p[i]=a[i],vis[a[i]]=1;
    61                 else p[i]=b[i],vis[b[i]]=1;
    62             }
    63             if(i==nxt2)
    64             {
    65                 if(!vis[a[i]]) p[i]=a[i],vis[a[i]]=1;
    66                 else p[i]=b[i],vis[b[i]]=1;
    67             }
    68         }
    69         int cnt[maxn],if_=0;
    70         for(int i=1;i<=n;i++) cnt[p[i]]++;
    71         for(int i=1;i<=n;i++) if(cnt[i]>1) if_=1;
    72         if(if_==1)
    73         {
    74             memset(vis,0,sizeof(vis));
    75             memset(p,0,sizeof(p));
    76             for(int i=1;i<=n;i++)
    77                 if(i!=nxt1&&i!=nxt2) p[i]=a[i],vis[p[i]]=1;
    78             for(int i=1;i<=n;i++)
    79             {
    80                 if(i==nxt1)    
    81                 {
    82                     if(!vis[b[i]]) p[i]=b[i],vis[b[i]]=1;
    83                     else p[i]=a[i],vis[a[i]]=1;
    84                 }
    85                 if(i==nxt2)
    86                 {
    87                     if(!vis[b[i]]) p[i]=b[i],vis[b[i]]=1;
    88                     else p[i]=a[i],vis[a[i]]=1;
    89                 }
    90             }
    91         }
    92     }
    93     for(int i=1;i<=n;i++)
    94         printf("%d ",p[i]);
    95     return 0;
    96 }
    View Code
  • 相关阅读:
    【LeetCode】206. Reverse Linked List
    【LeetCode】160. Intersection of Two Linked Lists
    【LeetCode】190. Reverse Bits
    【LeetCode】165. Compare Version Numbers
    继续深入《一张神奇的图》
    Base64编码简介
    证明任意两个正整数相等(伪命题)
    DEADBEEF
    汉诺塔问题
    字符编码(2)-- 程序中的编码
  • 原文地址:https://www.cnblogs.com/chen74123/p/7477852.html
Copyright © 2020-2023  润新知