Summer exams 5: sequence (Category discussed water issues)

topic:

Arrangement means a continuous range (from 1 to n), but not necessarily monotonically increasing sequence.

This question obviously can discuss classified according to different situations, but the situation is too jumbled, consider the nature of the problem, thereby reducing the difficulty discussed.

Essence: an arrangement is a wrong, then each of 1 to n in a certain number appears twice, there is a number appears 0 times. The correct sequence is the number can not appear by the number appears twice in into. Then after these two changes, compare with another person, if the sequence after another and change compared to only a thin one on the establishment. Because there are two changes each check, and then set up the output lexicographically smallest can.

How to deal with this type of classification discussion (water issues): analysis of the problem, drawing mind maps, a variety of data to check whether finished all the circumstances, and the code is implemented correctly.

 

#include<bits/stdc++.h>
using namespace std;
#define N 100005
int n,a[N],b[N],cnt=0,cnta=0,shu,vis[N],c[N],d=0;
int main()
{
    freopen("seq.in","r",stdin);
    freopen("seq.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),vis[a[i]]++;
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
        if(a[i]!=b[i]) cnt++;
    }
    if(cnt>=3) { printf("Impossible\n"); return 0; }
    for(int i=1;i<=n;i++) if(!vis[i]) cnta++,shu=i;
    if(cnta>1||cnta==0) { printf("Impossible\n"); return 0; }
    int fl=0,rt=0;
    for(int i=1;i<=n;i++)
    if(vis[a[i]]==2){
        int xx=a[i],cntb=0;
        a[i]=shu;
        for(int j=1;j<=n;j++)
        if(b[j]!=a[j]) cntb++;
        if(cntb==1&&rt==0){
            for(int j=1;j<=n;j++)
            c[j]=a[j];
            rt++;
        }
        else if(cntb==1) { rt++; break; }
        a[i]=xx;
    }
    if(!rt) { printf("Impossible\n"); return 0; }
    if(rt==1){
        for(int i=1;i<=n;i++) printf("%d\n",c[i]);
        return 0;
    }
    
    for(int j=1;j<=n;j++)
    if(c[j]>a[j]) { d=1; break; }
    else if(c[j]<a[j]) { d=0; break; }
    if(d){ for(int j=1;j<=n;j++) printf("%d\n",a[j]); }
    else { for(int j=1;j<=n;j++) printf("%d\n",c[j]); }
    
    return 0;
}
/*
5 
1 2 3 4 3 
1 2 5 4 5

5
5 4 5 3 1
4 4 2 3 1
*/

 

Guess you like

Origin www.cnblogs.com/mowanying/p/11414381.html