QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#497492 | #1646. Disk Sort | masterhuang | TL | 0ms | 3732kb | C++20 | 1.9kb | 2024-07-29 11:08:40 | 2024-07-29 11:08:41 |
Judging History
answer
#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
#define fi first
#define se second
#define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=1e3+5;
int n,s[N],L[N],tot;P w[N][3],ans[N*6];
struct node
{
int t,a[3];
inline auto& operator[](int x){return a[x];}
inline int sz(){return 2-t;}
inline void push(int x){a[t--]=x;}
inline int top(){return a[t+1];}
inline int pop(){return a[++t];}
}a[N];
inline int gete(){for(int i=1;i<=n;i++) if(!a[i].sz()) return i;}
inline void upd(int i,int j,int x){s[x]+=j;w[x][L[x]++]={j,i};}
inline bool chk(int i){return s[i]<=3&&(w[i][0].se!=w[i][1].se||w[i][0].se!=w[i][2].se||w[i][1].se!=w[i][2].se)&&!min({w[i][0].fi,w[i][1].fi,w[i][2].fi});}
inline int getc()
{
for(int i=1;i<=n;i++) s[i]=L[i]=0;
for(int i=1;i<=n;i++) if(a[i].sz())
for(int j=0;j<3;j++) upd(i,j,a[i][j]);
for(int i=1;i<=n;i++) if(chk(i)) return i;
return -1;
}
inline void mv(int x,int y){int t=a[x].pop();a[y].push(t);ans[++tot]={x,y};}
inline bool pd(int x){x=a[x].sz();return 1<=x&&x<=2;}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n;
for(int i=1;i<=n;i++) a[i].t=-1;a[n+1].t=2;
for(int i=0;i<3;i++) for(int j=1;j<=n;j++) cin>>a[j][i];n++;
for(int i=1;i<n-1;i++)
{
int z=gete(),c=getc(),b[3];sort(w[c],w[c]+3);
if(c==-1) break;
for(int j=0;j<3;j++) b[j]=w[c][j].se;
mv(b[0],z);int w=b[1];
while(a[w].top()^c) mv(w,b[0]);
mv(w,z);w=b[2];
while(a[w].top()^c) mv(w,b[a[b[0]].sz()==3||b[0]==w]);
mv(w,z);
while(pd(b[0])||pd(b[1])||pd(b[2]))
{
int t=0,c[3];
for(int j=0;j<3;j++) if(pd(b[j])) c[++t]=b[j];
sort(c+1,c+1+t,[](int x,int y){return a[x].sz()<a[y].sz();});
t=unique(c+1,c+1+t)-c-1;mv(c[1],c[2]);
}
}int w=gete();
if(w!=n) for(int i=1;i<=3;i++) mv(n,w);cout<<tot<<"\n";
for(int i=1;i<=tot;i++) cout<<ans[i].fi<<" "<<ans[i].se<<"\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
input:
4 2 3 1 4 2 1 1 4 2 3 3 4
output:
9 3 5 2 3 2 5 3 2 3 5 3 2 5 3 5 3 5 3
result:
ok n=4
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
2 1 2 1 2 1 2
output:
0
result:
ok n=2
Test #3:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
3 2 2 1 1 3 3 1 2 3
output:
11 3 4 1 3 1 4 1 4 2 1 3 1 2 3 2 1 4 2 4 2 4 2
result:
ok n=3
Test #4:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
3 2 1 3 2 1 1 2 3 3
output:
8 2 4 2 4 3 2 3 4 3 2 4 3 4 3 4 3
result:
ok n=3
Test #5:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
3 1 3 2 2 3 3 2 1 1
output:
12 3 4 1 3 1 4 1 4 2 1 2 1 3 2 3 1 3 2 4 3 4 3 4 3
result:
ok n=3
Test #6:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
3 3 1 1 2 3 2 3 1 2
output:
11 2 4 3 4 2 3 2 4 1 2 3 2 1 3 1 2 4 1 4 1 4 1
result:
ok n=3
Test #7:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
3 3 2 1 2 3 2 1 1 3
output:
13 2 4 1 2 1 4 3 1 3 4 3 1 1 3 2 3 2 3 2 1 4 2 4 2 4 2
result:
ok n=3
Test #8:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
3 1 2 3 2 1 2 1 3 3
output:
14 1 4 2 1 2 4 1 2 1 2 1 4 2 1 2 1 3 2 3 1 3 2 4 3 4 3 4 3
result:
ok n=3
Test #9:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
3 1 3 1 3 2 2 2 3 1
output:
13 1 4 3 4 3 1 3 4 1 3 2 1 2 3 1 2 1 2 1 3 4 1 4 1 4 1
result:
ok n=3
Test #10:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
3 2 3 1 3 1 3 2 2 1
output:
15 3 4 2 3 2 4 3 2 3 2 3 4 2 3 1 2 1 3 2 1 2 3 2 1 4 2 4 2 4 2
result:
ok n=3
Test #11:
score: 0
Accepted
time: 0ms
memory: 3732kb
input:
3 1 3 2 3 1 3 1 2 2
output:
14 1 4 2 1 2 4 1 2 1 2 1 4 2 1 2 1 3 2 3 1 3 2 4 3 4 3 4 3
result:
ok n=3
Test #12:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
3 1 2 3 3 1 1 2 3 2
output:
13 1 4 2 1 2 4 3 2 3 4 3 2 1 3 2 3 1 2 1 3 4 1 4 1 4 1
result:
ok n=3
Test #13:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
3 3 1 2 1 3 1 3 2 2
output:
14 2 4 1 2 1 4 3 1 3 4 3 1 1 3 1 3 2 1 2 1 2 3 4 2 4 2 4 2
result:
ok n=3
Test #14:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
3 1 2 3 3 3 1 1 2 2
output:
15 1 4 3 1 3 4 1 3 1 3 1 4 3 1 2 3 2 1 3 2 3 1 3 2 4 3 4 3 4 3
result:
ok n=3
Test #15:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
3 3 1 2 2 2 3 3 1 1
output:
14 3 4 1 3 1 4 2 1 2 4 2 1 1 2 1 2 3 1 3 1 3 2 4 3 4 3 4 3
result:
ok n=3
Test #16:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
3 1 3 2 2 2 3 1 3 1
output:
14 3 4 1 3 1 4 2 1 2 4 2 1 1 2 1 2 3 1 3 2 3 1 4 3 4 3 4 3
result:
ok n=3
Test #17:
score: 0
Accepted
time: 0ms
memory: 3724kb
input:
3 1 3 2 3 2 3 1 1 2
output:
15 3 4 2 3 2 4 3 2 3 2 3 4 2 3 1 2 1 3 2 1 2 3 2 1 4 2 4 2 4 2
result:
ok n=3
Test #18:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
4 2 2 3 1 2 3 3 4 1 4 4 1
output:
17 1 5 2 5 1 5 1 2 2 1 4 1 4 2 4 1 3 4 2 3 2 4 3 2 3 4 3 2 5 3 5 3 5 3
result:
ok n=4
Test #19:
score: -100
Time Limit Exceeded
input:
4 3 3 1 1 2 3 4 4 2 1 4 2