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

【codeforces 814B】An express train to reveries

2017-10-04 18:44 423 查看

【题目链接】:http://codeforces.com/contest/814/problem/B

【题意】

给你两个元素个数都为n的序列a[]和b[]
要求你构造出一个排列p(1..n);
使得p与a不同的元素个数恰好为1,p与b不同的元素个数也恰好为1;
保证a和b至少有一个元素不同;

【题解】

稍加分析就能知道;
a和b的不同元素最多只能有两个;
否则无解;
则对不同元素的个数为1个和2个的情况分类讨论就好;
方便起见假设不同的位置在i和j;i<j
个数为1的话;
p[i]与剩下n-1个数字都不一样就好;
个数为2的话;
p[i]=a[i],p[j]=b[i]
或者是p[i]=b[i],p[j] = a[j];
看看哪种符合要求就选那一种.

【Number Of WA】

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100;

int n;
int a
,b
,p
,cnt,bo
;
vector <int> v;

int main(){
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n;
rep1(i,1,n)
cin >> a[i];
rep1(i,1,n)
cin >> b[i];
rep1(i,1,n)
if (a[i]!=b[i]){
cnt++;
v.pb(i);
}
if (cnt==1){
int idx = v[0];
rep1(i,1,n)
if (i!=idx){
p[i] = a[i];
bo[a[i]] = 1;
}
rep1(i,1,n)
if (!bo[i])
p[idx] = i;
}else{
int idx1 = v[0],idx2 = v[1];
rep1(i,1,n)
if (i!=idx1 && i!=idx2){
p[i] = a[i];
bo[a[i]] = 1;
}
if (!bo[a[idx1]] && !bo[b[idx2]] && a[idx1]!=b[idx2]){
p[idx1] = a[idx1];
p[idx2] = b[idx2];
}
if (!bo[b[idx1]] && !bo[a[idx2]] && b[idx1] != a[idx2]){
p[idx1] = b[idx1];
p[idx2] = a[idx2];
}
}
rep1(i,1,n)
cout << p[i] <<' ';
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: