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 ...
随机推荐
- Eclipse dynamic web project 插件
下载了Eclipse Oxygen 发现没有Dynamic web Project 首先我们先了解下Dynamic Web Project If you want to create a c ...
- OC——继承
继承的其中一个很重要的目的是为了实现多态.我们现在先来看看OC的继承. 一.继承 父类: 头文件 // // Peason.h // 01-继承和多态 // // Created by zhangji ...
- POJ3069(贪心+巧用优先队列)
题目传送门:http://poj.org/problem?id=3069 题目大意:一个直线上有N个点.点i的距离是Xi.从这些点中选取若干个加上标记.要求:对于每个点,与其距离为R的范围内必有做标记 ...
- JS -- Variables As Properties
Variables As Properties When you declare a global JavaScript variable, what you are actually doing i ...
- ThinkPHP中:RBAC权限控制的实习步骤
使用版本ThinkPHP3.1.3 第一步,建表及数据 第二步,建关联模型 第三步,控制器使用关联模型.配置文件 第四步,模板显示数据 第一步,建表及数据 在数据库中,建立一个companysvn数据 ...
- Windows 编程中恼人的各种字符以及字符指针类型
在Windows编程中,很容易见到这些数据类型:LPSTR,LPTSTR,LPCTSTR... 像很多童鞋一样,当初在学Windows编程的时候,对着些数据类型真的是丈二和尚,摸不着头脑,长时间不用就 ...
- python之路第五篇之模块和加密算法(进阶篇:续)
模块 Python中,如果要引用一些内置的函数,该怎么处理呢?在Python中有一个概念叫做模块(module) 简单地说,模块就是一个保存了Python代码的文件. 模块分类: 1)内置模块 2)自 ...
- 认识jQuery的Promise
先前了解了ES6的Promise对象,来看看jQuery中的Promise,也就是jQuery的Deferred对象. 打开浏览器的控制台先. <script> var defer = $ ...
- docker命令不需要敲sudo的方法
由于docker daemon需要绑定到主机的Unix socket而不是普通的TCP端口,而Unix socket的属主为root用户,所以其他用户只有在命令前添加sudo选项才能执行相关操作. 如 ...
- 写一个ORM框架的第一步
新一次的内部提升开始了,如果您想写一个框架从Apache Commons DbUtils开始学习是一种不错的选择,我们先学习应用这个小“框架”再把源代码理解,然后写一个属于自己的ORM框架不是梦. 一 ...