题解:AT_arc110_c [ARC110C] Exoswap

· · 题解

题目链接:

#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <vector>
#define lnt long long
using namespace std;
int xx;char ff,chh;inline int read(){
    xx=ff=0;while(!isdigit(chh)){if(chh=='-'){ff=1;}chh=getchar();}
    while(isdigit(chh)){xx=(xx<<1)+(xx<<3)+chh-'0';chh=getchar();}return ff? -xx: xx;
}
const int N=1e6;
int n,use,date[N],zb[N];
vector<int> ans;
int main(){
    n=read();
    for(int i=1;i<=n;++i){
        date[i]=read();
        zb[date[i]]=i;
    }
    for(int i=1;i<=n;++i){
        if(i<=use && date[i]!=i){
            puts("-1");
            return 0;
        }
        use=zb[i]-1;
        for(int e=use;e>=i;--e){
            swap(date[e],date[e+1]);
            zb[date[e]]=e;
            zb[date[e+1]]=e+1;
            ans.push_back(e);
        }
    }
    if((int)ans.size()!=n-1){puts("-1");return 0;}
    for(auto i: ans){printf("%d\n",i);}
    return 0;
}