zoukankan      html  css  js  c++  java
  • B. An express train to reveries(Round 418)

    B. An express train to reveries
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.

    On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and ninclusive, and the two sequences were not identical, that is, at least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.

    Well, she almost had it all — each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 ≤ i ≤ n) such that ai ≠ pi, and exactly one j (1 ≤ j ≤ n) such that bj ≠ pj.

    For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.

    Input

    The first line of input contains a positive integer n (2 ≤ n ≤ 1 000) — the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.

    The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of colours in the first meteor outburst.

    The third line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of colours in the second meteor outburst. At least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.

    Output

    Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.

    Input guarantees that such permutation exists.

    Examples
    input
    5
    1 2 3 4 3
    1 2 5 4 5
    output
    1 2 5 4 3
    input
    5
    4 4 2 3 1
    5 4 5 3 1
    output
    5 4 2 3 1
    input
    4
    1 1 3 4
    1 4 3 4
    output
    1 2 3 4
    Note

    In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.

    In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.

     hint:题目给你两个长为n的序列,两个序列至少有一个元素不同,问你能不能用1-n n个数字构造出一个序列使得这个序列和给出的两个序列恰好有一个元素不相同。我们冷静分析一下可以发现,所给的两个中共有的元素一定要出现在我们构造的序列的对应位置,而且所给的两个序列最多只有两对数不同(可以简单证明一下),所以我们针对两个序列有一对数不同和两个序列有两队数不同进行讨论一下,这里其实乱搞的话要细心一点。。。(连我自己都不知道自己的代码为什么是对的。。。)看题解好像也是,多判断几种情况暴力乱搞。。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<string>
     5 #include<vector>
     6 #include<stack>
     7 #include<queue>
     8 #include<string.h>
     9 #include<stdlib.h>
    10 using namespace std;
    11 typedef long long ll;
    12  int a[1050], b[1050], vis[1050], c[1050];
    13 
    14  //智商不够用。。。多加几个判断,考虑情况要全面一点。。。
    15 int main(){
    16     int n;
    17     memset(vis, 0, sizeof(vis));
    18     memset(c, 0, sizeof(c));
    19     memset(a, 0, sizeof(a));
    20     memset(b, 0, sizeof(b));
    21     cin >> n;
    22     for(int i=1; i<=n; i++){
    23         cin >> a[i];
    24     }
    25     for(int i=1; i<=n; i++){
    26         cin >> b[i];
    27     }
    28     int cnt = 0;
    29     for(int i=1; i<=n; i++){
    30         if(a[i] == b[i]){
    31             cnt++;
    32             c[i] = a[i];
    33             vis[a[i]] = 1;
    34         }
    35     }
    36     if(n - cnt == 1){
    37         int key;
    38         for(int i=1; i<=n; i++){
    39             if(!vis[i]){
    40                 key = i;
    41             }
    42         }
    43         for(int i=1; i<=n;  i++){
    44             if(c[i] == 0) cout << key << " ";
    45             else cout << c[i] <<" ";
    46         }
    47     }else if(n - cnt == 2){
    48         int key[2], x=0, index[2];
    49         for(int i=1; i<=n; i++){
    50             if(!vis[i]){
    51                 key[x++] = i;
    52             }
    53         }
    54         x = 0;
    55         for(int i=1; i<=n; i++){
    56             if(a[i] != b[i]) index[x++] = i;
    57         }
    58         for(int i=1; i<=n;  i++){
    59             if(c[i] == 0){
    60                 if(!vis[b[i]] && !vis[a[i]]){
    61                     if(index[0] == i){
    62                         if(vis[a[index[1]]]) {
    63                             cout << a[i] << " ";
    64                             vis[a[i]] = 1;
    65                         }
    66                         else{
    67                             cout << b[i] << " ";
    68                             vis[b[i]] = 1;
    69                         }
    70                     }else{
    71                         if(vis[a[index[0]]]){
    72                             cout << a[i] << " ";
    73                             vis[a[i]] = 1;
    74                         }
    75                         else{
    76                             cout << b[i] << " ";
    77                             vis[b[i]] = 1;
    78                         }
    79                     }
    80                 }
    81                 else if(!vis[b[i]]){
    82                     cout << b[i] << " ";
    83                     vis[b[i]] = 1;
    84                 }else if(!vis[a[i]]){
    85                     cout << a[i] << " ";
    86                     vis[a[i]] = 1;
    87                 }
    88             }
    89             else cout << c[i] << " ";
    90         }
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    17.多线程
    15.collection
    Spark 论文篇-Spark:工作组上的集群计算的框架(中英双语)
    Redis相关面试题
    REDIS缓存穿透,缓存击穿,缓存雪崩原因+解决方案
    Jmeter使用函数助手生成随机数,同一个随机数多处使用
    ts-node与tsc
    vscode 自定义代码片段
    Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.22.2 from https://repo.maven.apache.org/maven2 was cached in the local repository
    Mac安装 nvm来管理node
  • 原文地址:https://www.cnblogs.com/ledoc/p/7086876.html
Copyright © 2011-2022 走看看