Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
34261 | 武云帆 | 【S】T2 | C++ | 运行超时 | 55 | 2978 MS | 840 KB | 8954 | 2024-11-04 10:56:45 |
#include<bits/stdc++.h> using namespace std; #define endl '\n' int T; int n,k; const int N=2000100; int p[N]; int a[N],b[N],c[N]; int w[N]; int main(){ //ios::sync_with_stdio(false); //cin.tie(0); //cout.tie(0); //freopen("9347.in","r",stdin); //freopen("9347.out","w",stdout); cin>>T; while(T--){ cin>>n>>k; for(int i=1;i<=n;i++){ cin>>p[i]; w[p[i]]=i; } if(n<=2){ if(p[1]==1) cout<<0<<endl; else cout<<-1<<endl; } else if(n==3){ if(p[1]==2) cout<<-1<<endl; else if(p[1]==3){ if(p[2]==2) cout<<-1<<endl; else{ cout<<2<<endl; cout<<1<<" "<<2<<" "<<3<<endl; cout<<1<<" "<<2<<" "<<3<<endl; } } else{ if(p[2]==2) cout<<0<<endl; else{ cout<<1<<endl; cout<<1<<" "<<2<<" "<<3<<endl; } } } else if(p[1]==1){ int q=0; for(int i=1;i<=n;i++){ if(w[i]!=i){ a[++q]=1; b[q]=min(i,w[i]); c[q]=max(i,w[i]); swap(p[i],p[w[i]]); swap(w[p[i]],w[p[w[i]]]); } } cout<<q<<endl; for(int i=1;i<=q;i++) cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<endl; } else if(n>=4){ int x=-1; int z=-1; if(p[n]==1){ cout<<-1<<endl; continue; } for(int i=1;i<=n;i++){ if(p[i]==1) x=i; else if(x!=-1&&p[1]>p[i]){ z=i; break; } } if(z!=-1){ //cout<<n-1<<endl; //cout<<1<<" "<<x<<" "<<b<<endl; int q=0; //cout<<"!1"<<endl; swap(w[p[1]],w[p[x]]); swap(p[1],p[x]); //for(int i=1;i<=n;i++) cout<<w[i]<<" "; //cout<<endl; //for(int i=1;i<=n;i++) cout<<p[i]<<" "; //cout<<endl; for(int i=1;i<=n;i++){ //cout<<1<<" "<<min(p[i],i)<<" "<<max(p[i],i)<<endl; if(w[i]!=i){ a[++q]=1; b[q]=min(w[i],i); c[q]=max(w[i],i); swap(p[i],p[w[i]]); swap(w[p[i]],w[p[w[i]]]); //for(int i=1;i<=n;i++) cout<<w[i]<<" "; //cout<<endl; //for(int i=1;i<=n;i++) cout<<p[i]<<" "; //cout<<endl; } } cout<<q+1<<endl; cout<<1<<" "<<x<<" "<<z<<endl; for(int i=1;i<=q;i++) cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<endl; } else if(p[1]>=3){ /*for(int i=1;i<=n;i++){ if(p[i]==2){ z=i; break; } }*/ int q=0; z=w[2]; a[++q]=1; b[q]=z; c[q]=n; a[++q]=1; b[q]=x; c[q]=n; swap(p[z],p[n]); swap(w[p[z]],w[p[n]]); swap(p[1],p[x]); swap(w[p[1]],w[p[x]]); for(int i=2;i<=n;i++){ if(p[i]!=i){ a[++q]=1; b[q]=min(i,w[i]); c[q]=max(i,w[i]); swap(p[i],p[w[i]]); swap(w[p[i]],w[p[w[i]]]); } } //cout<<"!2"<<endl; cout<<q<<endl; for(int i=1;i<=q;i++) cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<endl; } else{ int q=0; int y=-1; for(int i=2;i<=n;i++){ //cout<<p[i]<<" "; if(p[i]==n){ y=i; break; } } //cout<<y<<endl; if(y<x){ a[++q]=1; b[q]=y; c[q]=x; swap(p[1],p[y]); swap(w[p[1]],w[p[y]]); a[++q]=1; b[q]=x; c[q]=n; swap(p[1],p[x]); swap(w[p[1]],w[p[x]]); for(int i=1;i<=n;i++){ if(w[i]!=i){ a[++q]=1; b[q]=min(w[i],i); c[q]=max(w[i],i); swap(p[i],p[w[i]]); swap(w[p[i]],w[p[w[i]]]); } } //cout<<"!3"<<endl; cout<<q<<endl; for(int i=1;i<=q;i++) cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<endl; } else if(p[2]!=1){ a[++q]=1; b[q]=2; c[q]=y; swap(p[2],p[y]); swap(w[p[2]],w[p[y]]); a[++q]=1; b[q]=2; c[q]=x; swap(p[1],p[2]); swap(w[p[1]],w[p[2]]); a[++q]=1; b[q]=x; c[q]=n; swap(p[1],p[x]); swap(w[p[1]],w[p[x]]); for(int i=1;i<=n;i++){ if(w[i]!=i){ a[++q]=1; b[q]=min(w[i],i); c[q]=max(w[i],i); swap(p[i],p[w[i]]); swap(w[p[i]],w[p[w[i]]]); } } //cout<<"!4"<<endl; cout<<q<<endl; for(int i=1;i<=q;i++) cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<endl; } else{//p[1]=2 int t,qt; for(int i=n;i>=1;i--){ if(p[i]!=i){ t=i; break; } } qt=w[t]; /*for(int i=n;i>=1;i--){ if(p[i]==t){ qt=i; break; } }*/ if(t!=2){ int q=0; //cout<<t<<" "<<qt<<endl; a[++q]=1; b[q]=2; c[q]=qt; swap(p[2],p[qt]); swap(w[p[2]],w[p[qt]]); a[++q]=1; b[q]=2; c[q]=qt; swap(p[1],p[2]); swap(w[p[1]],w[p[2]]); a[++q]=1; b[q]=qt; c[q]=t; swap(p[1],p[qt]); swap(w[p[1]],w[p[qt]]); for(int i=1;i<=n;i++){ if(w[i]!=i){ a[++q]=1; b[q]=min(w[i],i); c[q]=max(w[i],i); swap(p[i],p[w[i]]); swap(w[p[i]],w[p[w[i]]]); } } //cout<<"!5"<<endl; cout<<q<<endl; for(int i=1;i<=q;i++) cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<endl; } else{ if(k>=5){ cout<<5<<endl; cout<<1<<" "<<3<<" "<<n<<endl; cout<<1<<" "<<2<<" "<<3<<endl; cout<<1<<" "<<2<<" "<<3<<endl; cout<<1<<" "<<3<<" "<<n<<endl; cout<<1<<" "<<3<<" "<<n<<endl; } else cout<<-1<<endl; } } } } } //cout.flush(); return 0; }