B. An express train to reveries
1 second
256 megabytes
standard input
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 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.
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 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.
5
1 2 3 4 3
1 2 5 4 5
1 2 5 4 3
5
4 4 2 3 1
5 4 5 3 1
5 4 2 3 1
4
1 1 3 4
1 4 3 4
1 2 3 4
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.
/*----------------------------------------------
File: F:\ACM源代码\code forces\418\B.cpp
Date: 2017/6/8 17:17:55
Author: LyuCheng
----------------------------------------------*/
/*
题意:给你序列a,b,让你构造一个1到n的全排列序列p,使得恰好有一个i使得ai!=pi,恰好有一个j使得bj!==pj
给出的样例保证有解
思路:考虑下来,a,b不相等的位置最多有两处,并且a,b序列中出现过的数一定是1-n中的n-1个
当有一处不同的时候:
直接将a中两个重复数字的其中一个数字换成没有出现过的那个数字,然后输出 当有两处不同的时候:
这种情况有很多种,但是简单粗暴的办法就是,先求出,在a,b序列中分别没出现过的数,有两个,然后
a[i]==b[i]的地方,p[i]=a[i],不相等的地方,先假定p[a,b不相等位置1]=tmp1,p[a,b不相等位置2]=tmp2,
然后加一个判断是否满足,输出的条件:恰好有一个i使得ai!=pi,恰好有一个j使得bj!==pj,如果不满足,就挑换位置
*/
#include <bits/stdc++.h>
#define MAXN 1005
#define LL long long
using namespace std; int n;
int a[MAXN];
int visa[MAXN];
int b[MAXN];
int visb[MAXN];
int c[MAXN];
int main(int argc, char *argv[])
{
// freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
visa[a[i]]=true;
}
for(int i=;i<n;i++){
scanf("%d",&b[i]);
visb[b[i]]=true;
}
vector<int>v;
for(int i=;i<n;i++){
if(a[i]==b[i]){
c[i]=a[i];
}else{
c[i]=;
v.push_back(i);
}
}
if(v.size()==){//只有一个的时候
for(int i=;i<=n;i++){
if(visa[i]==false&&visb[i]==false){
c[v[]]=i;
break;
}
}
}else{//有两个位置不同的时候
int tmp1,tmp2;
for(int i=;i<=n;i++){
if(visa[i]==false) tmp1=i;
if(visb[i]==false) tmp2=i;
}
c[v[]]=tmp1;
c[v[]]=tmp2;
int cur=;
for(int i=;i<n;i++){
if(a[i]!=c[i]){
cur++;
}
}
for(int i=;i<n;i++){
if(b[i]!=c[i]){
cur++;
}
}
if(cur>){
swap(c[v[]],c[v[]]);
}
}
for(int i=;i<n;i++){
printf(i==?"%d":" %d",c[i]);
}
printf("\n");
return ;
}
B. An express train to reveries的更多相关文章
- An express train to reveries
An express train to reveries time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #418 (Div. 2) B. An express train to reveries
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- codeforces 814B.An express train to reveries 解题报告
题目链接:http://codeforces.com/problemset/problem/814/B 题目意思:分别给定一个长度为 n 的不相同序列 a 和 b.这两个序列至少有 i 个位置(1 ≤ ...
- Codeforces - 814B - An express train to reveries - 构造
http://codeforces.com/problemset/problem/814/B 构造题烦死人,一开始我还记录一大堆信息来构造p数列,其实因为s数列只有两项相等,也正好缺了一项,那就把两种 ...
- CF814B An express train to reveries
思路: 模拟,枚举. 实现: #include <iostream> using namespace std; ; int a[N], b[N], cnt[N], n, x, y; int ...
- #418 Div2 Problem B An express train to reveries (构造 || 全排列序列特性)
题目链接:http://codeforces.com/contest/814/problem/B 题意 : 有一个给出两个含有 n 个数的序列 a 和 b, 这两个序列和(1~n)的其中一个全排列序列 ...
- Codeforces Round #418 (Div. 2) A+B+C!
终判才知道自己失了智.本场据说是chinese专场,可是请允许我吐槽一下题意! A. An abandoned sentiment from past shabi贪心手残for循环边界写错了竟然还过了 ...
- codeforces round 418 div2 补题 CF 814 A-E
A An abandoned sentiment from past 水题 #include<bits/stdc++.h> using namespace std; int a[300], ...
- AtCoder Express(数学+二分)
D - AtCoder Express Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement In ...
随机推荐
- Unity GameObject Class
GameObject Note : gameObject 指的是当前挂着的对象. class in UnityEngine / Inherits from:Object Descriptio ...
- day12<常见对象+>
常见对象(Scanner的概述和方法介绍) 常见对象(Scanner获取数据出现的小问题及解决方案) 常见对象(String类的概述) 常见对象(String类的构造方法) 常见对象(String类的 ...
- 每周分享之 二 http协议(2)
本次分享http协议,共分为三部分,这是第二部分,主要讲解请求与响应的字段,以及状态码. 以http/1.1版本的一个完整的请求与响应作为例子 http请求信息由三部分组成 1.请求方法(GET/PO ...
- hdu4705 Y 2013 Multi-University Training Contest 10
Y Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submis ...
- CentOS 7搭建LAMP环境(二)
前面已经讲过了CentOS 7下LAMP环境的配置过程,一台简单的WEB服务器已搭建完成,但后期在网站部署的过程中也许会碰到各种各样头疼的问题.下面我们来讲讲怎么解决这些问题,以及如何高效地管理服务器 ...
- linux视频之media媒体框架
linux视频媒体(kernel层分析)主要包括三个文件: (/drivers/media/media-device.c , /drivers/media/media-devnode.c , /dr ...
- jQuery实现web页面固定列表搜索
1.需求分析:现在有一个数据展示列表页面,列表内容固定,使用jQuery在固定的列表中实现搜索功能. 2.核心代码: <!-- 添加jquery库 --> <script type= ...
- HDU1411 欧拉四面体
用向量解决: 三角形面积:S=1/2*|x1*y2-x2*y1|; (粗体表示向量) 三棱锥体积:V=1/6*(OA*OB)*OC 不知道哪里去找的代码,毕竟很线性代数矩阵什么的很头疼,晚上 ...
- ServletListener对象学习笔记
JavaWeb学习笔记--监听器详解 知识概要: 1.监听器下例子举例 2.Servlet规范中的监听器 3. 4. 1. 监听器下例子举例说明: /* Frame:事件源.发生事件的对象 Windo ...
- hdu 4057--Rescue the Rabbit(AC自动机+状压DP)
题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...