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 ...
随机推荐
- Java 制作证书的工具keytool用法总结
一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...
- MongDB开启权限认证
在生产环境中MongoDB已经使用有一段时间了,但对于MongoDB的数据存储一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),最近在酷壳网看了一篇技术文章(https://cools ...
- Spring Boot 学习之项目构建
最近做了外包,都是工程专业术语,前期熟悉项目看文档看的挺累的,闲暇时间自己学习一下Spring Cloud,找点乐趣. 就有了下面的小项目. 本项目是一个Spring boot项目. 一.nginx做 ...
- 复选框demo
本篇文章是关于复选框的,有2种形式:1.全选.反选由2个按钮实现:2.全选.反选由一个按钮实现. <!DOCTYPE html> <html> <head> < ...
- Select()使用否?
David Treadwell ,Windows Socket 的一位开发者,曾经在他的一篇名为"Developing Transport-Independent Applications ...
- Flip Game poj 1753
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29731 Accepted: 12886 Descr ...
- (转)深度学习word2vec笔记之基础篇
深度学习word2vec笔记之基础篇 声明: 1)该博文是多位博主以及多位文档资料的主人所无私奉献的论文资料整理的.具体引用的资料请看参考文献.具体的版本声明也参考原文献 2)本文仅供学术交流,非商用 ...
- python采用 多进程/多线程/协程 写爬虫以及性能对比,牛逼的分分钟就将一个网站爬下来!
首先我们来了解下python中的进程,线程以及协程! 从计算机硬件角度: 计算机的核心是CPU,承担了所有的计算任务.一个CPU,在一个时间切片里只能运行一个程序. 从操作系统的角度: 进程和线程,都 ...
- spring boot 快速生成demo工程 官网生成
最近一直在弄springboot的项目,居然最近才知道快速生成springBoot工程,原来可以这么简单, 而且官网还提供了生成java或是web项目,需要jpa,模板等服务,直接一键集成.话不多说, ...
- Spring读书笔记——bean加载
我们的日常开发几乎离不开Spring,他为我们的开发带来了很大的便捷,那么Spring框架是如何做到方便他人的呢.今天就来说说bean如何被加载加载. 我们在xml文件中写过太多类似这样的bean声明 ...