Codeforces 814 B An express train to reveries

来源:互联网 发布:网站sql注入漏洞修复 编辑:程序博客网 时间:2024/05/21 19:44

题目地址:http://codeforces.com/contest/814/problem/B
题意:就是告诉你两个序列,每个序列都错了一位,让你求出一条原序列
思路:先对比两个序列,判断他是有一个地方不同还是两个地方不同

1、一个地方不同则什么就是这个等下错了,用一个标记数组去存每一个数的个数,再遍历一遍,如果有没有出现的就是把这个数添加进序列就好了。
2、两个地方不同就需要判断一下,如果两个序列的不同位置的数相同的或者该数都出现过在两个序列里的话话就是把另外两个数交换一下就好了,要不然就是这两个数交换。

详细看代码

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <algorithm>#define N 1010#define LL long long #define inf 0x3f3f3f3fusing namespace std;int main() {    cin.sync_with_stdio(false);    int n, a[N], b[N], c[N], num[N];    while (cin >> n) {        memset(num, 0, sizeof(num));        for (int i = 0; i < n; i++) {            cin >> a[i];        }        for (int i = 0; i < n; i++) {            cin >> b[i];        }        int ans = 0;        for (int i = 0; i < n; i++) {            if (a[i] != b[i]) {                ans++;            }        }        if (ans == 1) {            int cnt;            for (int i = 0; i < n; i++) {                if (a[i] == b[i]) {                    num[a[i]]++;                    c[i] = a[i];                }                else{                    cnt = i;                }            }            for (int i = 1; i <= n; i++) {                if (!num[i]) {                    c[cnt] = i;                    break;                }            }        }        else {            int ans = -1, cnt = -1;            for (int i = 0; i < n; i++) {                if (a[i] == b[i]) {                    num[a[i]]++;                    c[i] = a[i];                }                else {                    if (ans == -1) {                        ans = i;                    }                    else                        cnt = i;                }            }            if (a[ans] == b[cnt] || num[a[ans]] || num[b[cnt]]) {                c[ans] = b[ans];                c[cnt] = a[cnt];            }            else {                c[ans] = a[ans];                c[cnt] = b[cnt];            }        }        for (int i = 0; i < n; i++) {            cout << c[i] << " ";        }        cout << endl;    }    return 0;}
原创粉丝点击