CODEFORCES-814B (thinking)

Source: Internet
Author: User

Topic Links: codeforces-814b


Test Instructions: There are two sequences, which should have been 1~n, but there is a data error that requires the possible original sequence.


Problem : because the two sequence, A, a, is only one error, then change one of the sequence, you can change back to the original sequence. Put all the numbers in B in an array C (note that there may be only one or two elements in C), and put the other same number in the map so that you can judge the existence at any time. If there is only one element in C then iterate through the B array to find the missing number. If there are two elements in C, the wrong number can be determined based on the existence of the elements in the map according to the four undetermined elements (that is, the element of the unequal position in a, b).


#include <bits/stdc++.h> using namespace std;
const int MAXN = 1E3 + 5;

Map<int, int> m; int main () {    int n, A[MAXN], B[MAXN], I, k = 0, P[MAXN], C[MAXN];     cin>>n; &nbsp

;   for (i = 1; I <= n; i++) cin>>a[i];     for (i = 1; I <= n; i++) {        cin>>b[i];   & nbsp;     if (a[i]! = B[i]) c[k++] = i;       //Record a different number  & nbsp;      else m[b[i]] = 1;               //Record the same number    }     if (k = = 1) {     & nbsp;  for (i = 1; I <= n; i++) {            if (m[i] = = 0) b [c[0]] = i;           //change to missing number        &nbsP }    }     else{       //b a c[0] bit element, while the c[0 in B] Bit already or a[c[1]] has been repeated, can be changed         if ((m[a[c[0]] [= 0 && m[b[c[0]] = = 1) | |
[A[c[1]]] = = 1) b[c[0]] = a[c[0]];
        else b[c[1]] = a[c[1]];    }     for (i = 1; I <= n-1; i++) cout<<b[i]<< "";
cout<<b[i]<<endl;
    return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.