codeforces B. An express train to reveries

来源:互联网 发布:unity游戏优化 编辑:程序博客网 时间:2024/06/08 10:00

B. An express train to reveries
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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, …, bnrespectively. Meteoroids’ colours were also between 1 and n inclusive, 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[],包含1到n的n-1种数,要找出一组p[]使得包含1—n个数字,并且与a[],b[]分别恰有一个不同。
解体思路:先找出b串少的那个数t2,将其替换b[]中重复的位置的数,判断其是否满足条件,不行就下一个。

p:这样一个题目我断断续续做了快一天,究其原因:没有读懂题目!比如我一直在想反例:
1 2 2 3 4 5
1 2 4 3 4 6
这样子没有输出p[]满足怎么办。。其实题目没这么复杂,只需要考虑符合输出的情况。

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <cstdlib>using namespace std;int g[1005];int a[1005],b[1005],p[1005];int q[1005];int t2,n;bool ok(int i){    for(int j=1;j<=n;j++)        p[j]=b[j];    p[i]=t2;    int k1=0,k2=0,qq=1;    memset(q,0,sizeof(q));    for(int j=1;j<=n;j++)    {        if(p[j]!=a[j]) k1++;        if(p[j]!=b[j]) k2++;    }    if(k1!=1||k2!=1)        return false;    return true;}int main(){    memset(g,0,sizeof(g));    scanf("%d",&n);    for(int i=1; i<=n; i++)        scanf("%d",&a[i]);    for(int i=1; i<=n; i++)        scanf("%d",&b[i]);    for(int i=1; i<=n; i++)        g[b[i]]++;    for(int i=1; i<=n; i++)    {        if(g[i]==0)            t2=i;    }    for(int i=1; i<=n; i++)    {        if(g[b[i]]==2)        {            if(ok(i))                break;            else continue;        }    }    for(int i=1;i<n;i++)        printf("%d ",p[i]);    printf("%d\n",p[n]);    return 0;}
原创粉丝点击