[CF1375E]Inversion SwapSort

题目

传送门

题解

一道十分巧妙的构造题。

我们考虑先将所有数离散化,然后,对于一个数 \(i\),如果我们已经使得 \(1\sim i-1\) 有序(位置不一定相邻),现在要将 \(i\) 加入进去,维护这个序列有序,那么,我们的最终目的其实就是将 \(i\) 放到最后的位置去

我们考虑将 \(i\) 的位置 \(pos\)\(i-1\) 交换,然后再将 \(pos\)\(i-2\) 的位置交换...持续下去,直到 \(i-k\) 的位置小于 \(pos\) 停止

这样做可以使得序列合法,并且原序列中亦一定存在这样的逆序对,因为我们 \(1\sim i-1\) 中,无论谁在 \(pos\) 的后面,都一定是和 \(pos\) 的原数 \(i\) 存在逆序关系,而我们是从 \(1\) 开始往大枚举,那么所有在 \(i\) 后面的数(枚举保证是 \(1\sim i-1\)),都和 \(i\) 存在逆序关系,所以这样的方案一定存在

换句话说,这道题根本没有无解,输出 -1 是没用的

代码

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=1e3;
int a[MAXN+5],n;

inline void Init(){
    n=read(1);
    rep(i,1,n)a[i]=read(1);
}

inline bool cmp(const int i,const int j){
    if(a[i]==a[j])return i<j;
    return a[i]<a[j];
}
int t[MAXN+5];
inline void hasA(){
    rep(i,1,n)t[i]=i;
    sort(t+1,t+n+1,cmp);
    int hasnum=0;
    rep(i,1,n)a[t[i]]=++hasnum;
}

int pos[MAXN+5];
inline void Getpos(){rep(i,1,n)pos[a[i]]=i;}

int arr[MAXN+5];

int cnt,step[MAXN*MAXN+5][2];

signed main(){
    Init();
    hasA();
    Getpos();
    int nowpos,ind;
    rep(t,1,n){
        nowpos=pos[t];
        // printf("nowpos == %d\n",nowpos);
        if(nowpos>arr[t-1]){//它本来就在最后一个位置了
            // printf("situ 1\n");
            arr[t]=nowpos;continue;
        }
        ind=0;
        while(arr[++ind]<=nowpos);
        // printf("ind == %d\n",ind);
        fep(i,t-1,ind){
            ++cnt;
            step[cnt][0]=nowpos,step[cnt][1]=arr[i];
            swap(a[nowpos],a[arr[i]]);
        }
        fep(i,t,ind+1)arr[i]=arr[i-1];
        arr[ind]=nowpos;
        // rep(i,1,t)writc(arr[i],' ');Endl;
    }
    writc(cnt,'\n');
    rep(i,1,cnt)printf("%d %d\n",step[i][0],step[i][1]);
	return 0;
}
posted @ 2020-07-20 09:24  Arextre  阅读(130)  评论(0编辑  收藏  举报