您的位置:首页 > 大数据 > 人工智能

B. An express train to reveries

2017-08-03 18:08 281 查看
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 from1
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 withn meteorids,
colours of which being integer sequencesa1, a2, ..., an andb1, b2, ..., bn
respectively. Meteoroids' colours were also between1 and
n inclusive, and the two sequences were not identical, that is, at least onei (1 ≤ i ≤ n) exists, such thatai ≠ bi
holds.

Well, she almost had it all — each of the sequences a andb matched exactly
n - 1 elements in Sengoku's permutation. In other words, there is exactly onei (1 ≤ i ≤ n) such thatai ≠ pi,
and exactly onej (1 ≤ j ≤ n) such thatbj ≠ 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 integersa1, a2, ..., an
(1 ≤ ai ≤ n) — the sequence of colours in the first meteor outburst.

The third line contains n space-separated integersb1, b2, ..., bn
(1 ≤ bi ≤ n) — the sequence of colours in the second meteor outburst. At least one
i (1 ≤ i ≤ n) exists, such thatai ≠ 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.

题意:给你a,b两个串,求p串,要求p串分别和a,b串只有一个不相同,并且p串不能含有重复元素(必须1~n)题目中这点没有说的特别明确。。。

思路:

看案例可以知道,最多有两处不同,所以分开处理一处不同和两处不同的情况。不难发现,一处情况不同时,肯定是那个未出现过得数字,所以这种情况比较好处理。当有两处不同时,首先将两处相同的数字做标记,然后找到剩下的,分别放在第一次不同位置和第二位置,如果不成立,那么在交换两个数字的位置,直接输出就好了,因为题目中说一定有结果!!!

代码:

#include<bits/stdc++.h>
#define N 2005
using namespace std;
int a
, b
, used
, p
;
bool judge(int n) //判断
{
int num1, num2;
num1= num2=0;
for(int i = 1; i <= n; i++)
{
if(p[i] != a[i]) num1++;
if(p[i] != b[i]) num2++;
}
if(num1==num2&&num1 == 1) return 1;
return 0;
}

int main()
{
int n;
memset(used, 0, sizeof(used));
cin>>n;
for(int i = 1; i <= n; i++) cin>>a[i];
for(int i = 1; i <= n; i++) cin>>b[i];
int num=0;
for(int i = 1; i <= n; i++)
{
if(a[i]!=b[i])
num++;
else
used[a[i]]=1;
}
if(num == 1)  //一个不同处的情况
{
for(int i = 1; i <= n; i++)
{
if(a[i] == b[i])
{
cout<<a[i];
if(i==n)
cout<<endl;
else
cout<<' ';
}
else
{
for(int j = 1; j <= n; j++)
{
if(used[j]) continue;
cout<<j;
if(j==n)
cout<<endl;
else cout<<' ';
break;
}
}
}
}
else
{
stack <int> s;     //一开始用的向量,但栈是先进先出,方便
for(int i = 1; i <= n; i++)
if(!used[i]) s.push(i);
int num1 = s.top(); s.pop();
int num2 = s.top(); s.pop();
while(!s.empty()) s.pop();
for(int i = 1; i <= n; i++)
{
if(a[i] != b[i]) s.push(i);
else p[i] = a[i];
}
int xx = s.top(); s.pop();
int yy = s.top(); s.pop();
while(!s.empty()) s.pop();
p[xx] = num1;
p[yy] = num2;
if(judge(n))
{
for(int i = 1; i <= n; i++)
{
cout<<p[i];
if(i==n)
cout<<endl;
else
cout<<' ';
}
}
else
{
p[xx] = num2;
p[yy] = num1;
for(int i = 1; i <= n; i++)
{
cout<<p[i];
if(i==n)
cout<<endl;
else
cout<<' ';
}
}
}
return 0;
}

dfs

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
using namespace std;
int n,num,a[1005],b[1005],c[1005],d[1005];
bool flag,ok[1005];
bool work()
{
int n1=0,n2=0;
for(int i=1;i<=n;i++)
{
if(a[i]!=d[i]) n1++;
if(b[i]!=d[i]) n2++;
}
if(n1!=1||n2!=1) return false;
return true;
}
void dfs(int i)
{
if(flag) return;
for(int j=1;j<=n;j++)
{
if(ok[j]) continue;
d[c[i]]=j;
ok[j]=true;
if(i==num)
{
if(work())
{
flag=true;
return;
}
}
else
{
dfs(i+1);
if(flag) return;
}
ok[j]=false;
}
}
int main()
{
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
num=0;
memset(ok,false,sizeof(ok));
for(i=1;i<=n;i++)
{
if(a[i]!=b[i])
{
num++;
c[num]=i;
d[i]=0;
}
else
{
d[i]=a[i];
ok[d[i]]=true;
}
}
flag=false;
dfs(1);
for(i=1;i<=n;i++)
{
printf("%d ",d[i]);
}
return 0;
}


心得:

比赛中未能做出来。。。脑子混乱,没有解题那种思维意识。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: