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 ...
随机推荐
- Python 接口测试(五)
五:使用python进行组织编写接口测试用例 接口测试其实就是几个步骤. 拿到接口的url地址 查看接口是用什么方式发送 添加请求头,请求体 发送查看返回结果,校验返回结果是否正确 明白了接口测试的测 ...
- 杂谈--DML触发器学习
触发器按类型分为三类: 1. DML 触发器,在数据变更时触发: 2. DDL 触发器,在修改数据库级别或实例级别对象时触发: 3. Login 触发器,在用户登录时触发: 最常见的是DML触发器,D ...
- canvas浅谈 实现简单的自旋转下落
旋转和平移是2个基础的动画效果,也是复杂动画的基础. 如果是普通的页面只要设置transform属性很容易实现平移+旋转的组合效果,达到自旋转下落的效果.因为操作的直接是动作元素本身很容易理解. 但是 ...
- 好用的前端页面性能检测工具—sitespeed.io
引言 最近在做HTTP2技术相关调研,想确认一下HTTP2在什么情境下性能会比HTTP1.x有显著提升,当我把http2的本地环境(nginx+PHP)部署完成后进行相关测试时,我遇到了以下问题: ( ...
- Disharmony Trees 树状数组
Disharmony Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- 支持向量机(Support Vector Machine)-----SVM之SMO算法(转)
此文转自两篇博文 有修改 序列最小优化算法(英语:Sequential minimal optimization, SMO)是一种用于解决支持向量机训练过程中所产生优化问题的算法.SMO由微软研究院的 ...
- WinForm事件中的Object sender和EventArgs e参数
Windows程序有一个事件机制.用于处理用户事件. 在WinForm中我们经常需要给控件添加事件.例如给一个Button按钮添加一个Click点击事件.给TextBox文本框添加一个KeyPress ...
- PHP字符串函数-trim()实例用法
string trim ( string $str [, string $charlist = " \t\n\r\0\x0B" ] )此函数返回字符串 str 去除首尾空白字符后的 ...
- python中strip函数的用法
python中往往使用剥除函数strip()来对用户的输入进行清理.strip函数的最一般形式为: str.strip('序列') 其中,序列是一段字符串,该函数表示从头或者从尾部开始进行扫描,如果扫 ...
- 单独创建一个Android Test Project 时junit 的配置和使用
现在的集成ADT后Eclipse都可以直接创建Android Test Project 如图所示: 命名后选择你要测试的单元程序,比如我自己准备测试sms,便可以如图所示那样选择 本人新建的测试工程为 ...