An express train to reveries

本文解析了CodeForces上的一道B级题目,题目要求根据两个颜色序列找到可能的排列,通过分析不同情况下的解决方案,给出了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/814/B

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.


题目大意:首先输入整数n,表示序列a、b的长度。序列a、b满足:元素取值范围为1~n,至少有一个对应位置不相等的元素。题目要求求序列p,序列p满足:恰好有一个和a相等的元素,恰好有一个与b相等的元素。输出序列p。

解题思路:据题意,只能有两种情况:1 a、b不相等元素个数为1;2 a、b不相等元素个数为2。   如果不相等元素个数为1:则找出数组a中不存在的数(1~n),替换掉数组a中的对应位置的元素,然后输出数组a即为答案。如果不相等元素个数为2:则有分两种情况,找出没有重复元素的那种情况即为答案。

代码:

 

#include<iostream>
#include<map>
using namespace std;
int main()
{
    int a[1005],b[1005],p[1005],n,j,q;
    while(cin>>n)
    {
        map<int,int>M;
        int z=0,sum=0,i1,i2;
        for(int i=0;i<=n-1;i++)
            cin>>a[i];
        for(int i=0;i<=n-1;i++)
            cin>>b[i];
        for(int i=0;i<=n-1;i++)
        {
            if(a[i]!=b[i]&&z==1)
            {
                i2=i;
                sum++;
            }
            if(a[i]!=b[i]&&z==0)
            {
                z=1;
                i1=i;
                sum++;
            }
        }
        if(sum==1)
        {
            for(int i=1;i<=n;i++)
            {
                for(j=0;j<=n-1;j++)
                {
                    if(i==a[j])
                        break;
                }
                if(j>n-1)
                {
                    q=i;
                    break;
                }
            }
            for(int i=0;i<=i1-1;i++)
                cout<<a[i]<<' ';
            a[i1]=q;
            for(int i=i1;i<=n-1;i++)
                cout<<a[i]<<' ';
            cout<<endl;
        }
        if(sum==2)
        {
            for(int i=0;i<=n-1;i++)
            {
                if(i==i1||i==i2)
                    i++;
                p[i]=a[i];
            }
            p[i1]=a[i1];
            p[i2]=b[i2];
            int zz=1;
            for(int i=0;i<=n-1;i++)
            {
                M[p[i]]++;
                if(M[p[i]]>=2)
                {
                    zz=0;
                    break;
                }
            }
            if(zz==0)
            {
                for(int i=0;i<=n-1;i++)
                {
                    if(i==i1)
                        cout<<b[i1]<<' ';
                    else if(i==i2)
                        cout<<a[i2]<<' ';
                    else
                        cout<<a[i]<<' ';
                }
                cout<<endl;
            }
            else
            {
                for(int i=0;i<=n-1;i++)
                    cout<<p[i]<<' ';
                cout<<endl;
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值