Codeforces Round #418 (Div. 2) B. An express train to reveries

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.

比赛时比较匆忙,没有看清题意,好在C题刚做过类似的题,在没有优化下险过,不然和爆零没什么区别了

优化起来有点麻烦,就算咯~

继续说B题:

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.

这句话是说a数组和p数组仅有一个不同的元素,b数组也是。看懂了就简单啦!

遍历一遍a数组,找出缺少的那个元素t,再将a数组中重复的两个元素其中一个换成t,看一下和b数组是否满足题意即可。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack>
#define ll long long
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define cls(name,x) memset(name,x,sizeof(name))
using namespace std;
const int inf=1<<28;
const int maxn=1010;
const int maxm=2010;
const int mod=1e9+7;
const double pi=acos(-1.0);
int n;
int num1[maxn];
int num2[maxn];
bool judge(int a[],int b[])
{
    int c=0;
    for(int i=1;i<=n;i++)
        if(a[i]!=b[i]) c++;
    if(c==1) return true;
    else return false;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&num1[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&num2[i]);
        int vis[maxn];
        cls(vis,0);
        for(int i=1;i<=n;i++)
            vis[num1[i]]++;
        int t;
        for(int i=1;i<=n;i++)
            if(!vis[i]) t=i;
        for(int i=1;i<=n;i++)
            if(vis[num1[i]]==2)
            {
                int old=num1[i];
                num1[i]=t;
                if(judge(num1,num2)) break;
                else num1[i]=old;
            }
        for(int i=1;i<=n;i++)
            printf("%d%s",num1[i],i==n?"
":" ");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/mgz-/p/6959670.html