QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#466331#8872. Jumbled StacksPhantom Threshold (Jiachen Tang, Changdong Li, Weinuo Li)#AC ✓1ms3916kbC++175.9kb2024-07-07 18:29:202024-07-07 18:29:21

Judging History

This is the latest submission verdict.

  • [2024-07-07 18:29:21]
  • Judged
  • Verdict: AC
  • Time: 1ms
  • Memory: 3916kb
  • [2024-07-07 18:29:20]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

int n,k;
int stk[150][150];
int tp[150];
int sz[150];
int sum[150];

int tl[150],tr[150];
int rest[150];

vector<pair<int,int>> ans;

//#define DEBUG 1

#ifdef DEBUG
void display(){
	cerr << "---------" << endl;
	for (int i=1;i<=k;i++){
		for (int j=1;j<=sz[i];j++){
			cerr << stk[i][j] << " ";	
		}
		cerr << endl;
	}
	cerr << "--------" << endl;
}
#endif

void check(){
	for (int i=1;i<=k;i++){
		for (int j=1;j<=tp[i];j++){
			assert(stk[i][j]==tl[i]+j-1);	
		}
	}
}

void answer(){
	for (auto [x,y]:ans) cout << x << " " << y << "\n";
	cout << 0 << " " << 0 << "\n";
	check();
	#ifdef DEBUG
		display();
	#endif
	exit(0);	
}

bool move(int x,int y){
	if (x==y) return false;
	if (tp[x]==0) return false;
	if (tp[y]==sz[y]) return false;
	ans.emplace_back(x,y);
	stk[y][++tp[y]]=stk[x][tp[x]];
	stk[x][tp[x]--]=0;
	return true;	
}
pair<int,int> find(int x){
	for (int i=1;i<=k;i++){
		for (int j=1;j<=tp[i];j++) if (stk[i][j]==x) return make_pair(i,j);	
	}
	return make_pair(0,0);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin >> n >> k;
	for (int i=1;i<=k;i++){
		cin >> sz[i];
		for (int j=1;j<=sz[i];j++){
			cin >> stk[i][j];
			if (stk[i][j]!=0) tp[i]=j;
		}
	}
	
	int pos=1;
	for (int i=1;i<=k;i++) if (sz[i]>sz[pos]) pos=i;
	for (int i=1;i<=k;i++){
		while (move(pos,i));
	}
	#ifdef DEBUG
	display();
	#endif
	
	for (int i=1;i<=k;i++) sum[i]=sum[i-1]+sz[i];
	int mx=0;
	for (int i=1;i<=k;i++){
		tl[i]=1;
		tr[i]=0;	
	}
	for (int i=1;i<=k;i++){
		tl[i]=sum[i-1]+1;
		tr[i]=sum[i];
		if (tr[i]>=n) tr[i]=n;
		if (tr[i]==n){
			mx=i;
			break;	
		}
	}
	for (int i=1;i<=k;i++) rest[i]=sz[i]-(tr[i]-tl[i]+1);
	
	#ifdef DEBUG
		cerr << "-----tl,tr,rest-" << endl;
		for (int i=1;i<=k;i++) cerr << tl[i] << " " << tr[i] << " " << rest[i] << endl;
		cerr << "------" << endl;
	#endif
	
	for (int i=1;i<=mx;i++){
		if (i==pos) continue;
		for (int x=tl[i];x<=tr[i];x++){
			int xp=x-tl[i]+1;
			{
				auto [loci,locj]=find(x);
				#ifdef DEBUG
					cerr << "li,lj : " << loci << " " << locj << endl;
				#endif
				if (loci!=i){
					if (tp[i]==sz[i]) move(i,pos);
					while (tp[loci]>locj) move(loci,pos);
					move(loci,i);
					while (move(pos,loci));
				}
			}
			#ifdef DEBUG
			cerr << "x,xp : " << x << " " << xp << endl;
			display();
			#endif
			{
				int sec=0;
				for (int cc=1;cc<=k;cc++) if (cc!=i && cc!=pos){sec=cc;break;}
				#ifdef DEBUG
					cerr << "sec : " << sec << endl;
				#endif
				auto [loci,locj]=find(x);
				assert(loci==i);
				int flag=0;
				if (move(sec,pos)) flag=1;
				while (tp[loci]>locj) move(loci,pos);
				move(loci,sec);
				while (tp[loci]>=xp) move(loci,pos);
				move(sec,loci);
				while (tp[pos]>flag && move(pos,loci));
				move(pos,sec);
			}
			#ifdef DEBUG
			cerr << "x,xp : " << x << " " << xp << endl;
			display();
			#endif
		}
	}
	if (pos>mx) answer();
	for (int i=1;i<=k;i++){
		if (i==pos) continue;
		while (tp[i]>tr[i]-tl[i]+1) move(i,pos);	
	}
	#ifdef DEBUG
	cerr << "----------after-------" << endl;
	display();
	#endif
	vector<int> lis1;
	vector<int> lis2;
	for (int i=1;i<=k;i++){
		if (i==pos) continue;
		if (tp[i]==sz[i]) lis1.push_back(i);
		else lis2.push_back(i);
	}
	#ifdef DEBUG
	cerr << "lis1 : ";
	for (auto x:lis1) cerr << x << " ";
	cerr << endl;
	cerr << "lis2 : ";
	for (auto x:lis2) cerr << x << " ";
	cerr << endl;
	#endif
	if (lis1.size()){
		#ifdef DEBUG
			cerr << "case 1" << endl;
		#endif
		int sec=lis1[0];
		#ifdef DEBUG
			cerr << "sec : " << sec << endl;
		#endif
		for (int x=tl[pos];x<=tr[pos];x++){
			int xp=x-tl[pos]+1;
			auto [loci,locj]=find(x);
			assert(loci==pos);
			
			#ifdef DEBUG
			cerr << "x,xp : " << x << " " << xp << endl;
			cerr << "loci,locj : " << loci << " " << locj << endl;
			#endif
			
			move(sec,lis2[0]);
			int now=0;
			while (tp[pos]>locj){
				while (!move(pos,lis2[now])) now++;
			}
			#ifdef DEBUG
			display();
			#endif
			move(pos,sec);
			while (tp[pos]>=xp){
				while (!move(pos,lis2[now])) now++;
			}
			#ifdef DEBUG
			display();
			#endif
			move(sec,pos);
			for (int i=(int)lis2.size()-1;i>=0;i--){
				int tmp=lis2[i];
				while (tp[tmp]>tr[tmp]-tl[tmp]+1+(i==0) && move(tmp,pos));
			}
			move(lis2[0],sec);
			#ifdef DEBUG
			display();
			#endif
		}
		answer();
	}
	else{
		#ifdef DEBUG
			cerr << "case 2" << endl;
		#endif
		int mn=lis2[0];
		for (auto tmp:lis2) if (rest[tmp]<rest[mn]) mn=tmp;
		
		int szy=rest[mn];
		int szx=-rest[mn];
		for (auto tmp:lis2) szx+=rest[tmp];
		 
		for (int x=tl[pos];x<=tr[pos];x++){
			int xp=x-tl[pos]+1;
			{
				auto [loci,locj]=find(x);
				if (tp[pos]-locj<szy-1 && locj-xp>szx){
					
					for (;tp[pos]>=locj;) move(pos,mn);
					int now=0;
					for (int cc=1;cc<=szx;cc++){
						while (lis2[now]==mn || !move(pos,lis2[now])) now++;	
					}
					for (;tp[pos]>=xp;) move(pos,mn);
					for (;tp[mn]>tr[mn]-tl[mn]+1;) move(mn,pos);
					for (auto tmp:lis2){
						while (tp[tmp]>tr[tmp]-tl[tmp]+1) move(tmp,pos);	
					}
				}
			}
			{
				auto [loci,locj]=find(x);
				if (tp[pos]-locj>=szy-1){
					int now=0;
					for (;tp[pos]-locj>szy-1;){
						while (lis2[now]==mn || !move(pos,lis2[now])) now++;	
					}
					for (int cc=1;cc<=szy;cc++) move(pos,mn);
					for (;tp[pos]>=xp;){
						while (lis2[now]==mn || !move(pos,lis2[now])) now++;	
					}
					for (int cc=1;cc<=szy;cc++) move(mn,pos);
					for (auto tmp:lis2){
						while (tp[tmp]>tr[tmp]-tl[tmp]+1) move(tmp,pos);	
					}
				}
				else if (locj-xp<=szx){
					for (;tp[pos]>=locj;) move(pos,mn);
					int now=0;
					for (;tp[pos]>=xp;){
						while (lis2[now]==mn || !move(pos,lis2[now])) now++;	
					}
					for (;tp[mn]>tr[mn]-tl[mn]+1;) move(mn,pos);
					for (auto tmp:lis2){
						while (tp[tmp]>tr[tmp]-tl[tmp]+1) move(tmp,pos);	
					}
				}
			}
		}
	}
	answer();
	return 0;	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3840kb

input:

20 3
1 0
20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

2 1
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
3 2
1 3
3 1
2 3
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
1 3
2 1
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
1 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
...

result:

ok correct plan!

Test #2:

score: 0
Accepted
time: 0ms
memory: 3580kb

input:

30 5
22 28 15 25 1 18 30 5 10 2 12 13 14 21 0 0 0 0 0 0 0 0 0
10 29 6 16 24 0 0 0 0 0 0
11 26 20 11 23 19 4 0 0 0 0 0
6 9 3 17 0 0 0
10 27 8 22 7 0 0 0 0 0 0

output:

1 2
1 2
1 2
1 2
1 2
1 2
1 3
1 3
1 3
1 3
1 3
1 4
1 4
2 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 2
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
3 1
2 3
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
3 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 3
3 1
2 1
2 1
2 1
2 1
2 1
2 3
2 1
2 1
2 1
3 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 3
2 1
3 1
3 2
...

result:

ok correct plan!

Test #3:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

30 3
1 0
30 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

2 1
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
3 2
1 3
3 1
2 3
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
1 3
2 1
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
...

result:

ok correct plan!

Test #4:

score: 0
Accepted
time: 1ms
memory: 3724kb

input:

100 5
58 59 37 45 66 42 50 91 21 48 62 79 2 77 82 55 51 27 73 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
45 71 5 31 97 74 67 3 52 81 39 44 88 41 18 10 80 100 94 70 57 38 35 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
38 78 98 61 53 43 86 40 14 64 6 28 33 49 34 ...

output:

1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
3 1
2 3
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
3 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
...

result:

ok correct plan!

Test #5:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

93 69
2 93 71
1 7
1 23
2 44 32
1 1
2 17 20
1 73
1 55
1 82
1 54
2 80 49
1 72
2 77 57
3 89 51 53
1 58
1 65
1 36
3 38 64 0
2 52 6
2 8 43
1 16
1 35
1 30
1 15
1 12
1 41
1 79
1 28
2 60 29
1 4
1 46
1 13
1 26
2 63 25
1 11
1 18
1 31
1 37
1 59
4 47 21 66 86
1 19
1 0
1 39
1 83
1 91
1 78
1 76
1 68
1 75
1 24
1 4...

output:

40 18
40 42
40 67
40 68
1 40
5 1
40 5
2 40
1 2
1 40
2 1
40 1
40 2
1 40
52 1
40 52
2 40
1 2
2 1
40 2
2 40
64 40
64 40
64 2
40 64
40 64
40 64
1 40
2 1
1 2
40 1
3 40
30 3
40 30
1 40
3 1
1 3
40 1
4 40
52 40
52 4
40 52
40 52
1 40
4 1
4 40
1 4
40 4
40 1
4 40
19 4
40 19
1 40
4 1
1 4
40 1
5 40
64 5
40 64
1 ...

result:

ok correct plan!

Test #6:

score: 0
Accepted
time: 1ms
memory: 3632kb

input:

89 7
15 75 35 3 87 61 17 72 64 74 34 0 0 0 0 0
8 10 27 2 47 39 46 0 0
18 55 68 28 78 73 48 85 66 7 8 25 41 56 49 42 0 0 0
18 88 71 24 13 57 62 38 29 79 15 32 22 69 83 50 45 0 0
18 59 11 84 60 77 43 9 5 20 33 37 44 54 4 80 0 0 0
19 26 19 16 30 76 23 31 70 6 58 18 51 14 81 53 36 40 0 0
12 52 89 67 82 ...

output:

6 1
6 1
6 1
6 1
6 1
6 2
6 2
6 3
6 3
6 3
6 4
6 4
6 5
6 5
6 5
6 7
6 7
1 6
7 6
7 6
7 6
7 6
7 6
7 6
7 1
6 7
6 7
6 7
6 7
6 7
6 7
6 7
2 6
1 2
1 6
1 6
1 6
1 6
1 6
1 6
1 6
1 6
1 6
1 6
1 6
1 6
1 6
1 6
2 1
6 1
6 1
6 1
6 1
6 1
6 1
6 1
6 1
6 1
6 1
6 1
6 1
6 1
6 1
6 2
1 6
2 6
2 6
2 6
2 6
2 6
2 1
6 2
6 2
6 2
6 2
...

result:

ok correct plan!

Test #7:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

100 51
2 45 85
2 77 31
2 92 20
2 99 51
2 54 11
2 29 21
2 15 95
2 97 23
2 9 72
2 67 39
2 82 16
2 93 4
2 52 0
2 46 88
2 33 0
2 48 10
2 63 94
2 17 36
2 40 69
2 100 83
2 78 7
2 75 57
2 60 43
2 3 64
2 42 8
2 32 34
2 22 26
2 80 18
2 55 74
2 66 41
2 90 58
2 37 98
2 62 76
2 87 12
2 6 27
2 2 61
2 38 59
2 49 ...

output:

1 13
1 15
2 1
24 1
24 2
1 24
1 24
3 1
2 3
2 1
3 2
1 2
1 3
2 1
12 2
1 12
3 1
2 3
3 2
1 3
3 1
49 1
49 3
1 49
1 49
2 1
3 2
3 1
2 3
1 3
1 2
3 1
35 1
35 3
1 35
1 35
2 1
3 2
2 3
1 2
4 1
21 4
1 21
2 1
4 2
4 1
2 4
1 4
1 2
4 1
25 4
1 25
2 1
4 2
2 4
1 2
5 1
9 1
9 5
1 9
1 9
2 1
5 2
5 1
2 5
1 5
1 2
5 1
16 5
1 1...

result:

ok correct plan!

Test #8:

score: 0
Accepted
time: 0ms
memory: 3660kb

input:

100 4
46 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
28 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73
42 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 ...

output:

2 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 1
3 2
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
3 1
2 3
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
3 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
...

result:

ok correct plan!

Test #9:

score: 0
Accepted
time: 0ms
memory: 3916kb

input:

99 100
1 99
1 98
1 97
1 96
1 95
1 94
1 93
1 92
1 91
1 90
1 89
1 88
1 87
1 86
1 85
1 84
1 83
1 82
1 81
1 80
1 79
1 78
1 77
1 76
1 75
1 74
1 73
1 72
1 71
1 70
1 69
1 68
1 67
1 66
1 65
1 64
1 63
1 62
1 61
1 60
1 59
1 58
1 57
1 56
1 55
1 54
1 53
1 52
1 51
1 50
1 49
1 48
1 47
1 46
1 45
1 44
1 43
1 42
1 4...

output:

1 100
2 1
98 2
1 98
3 1
2 3
3 2
1 3
3 1
97 3
1 97
2 1
3 2
2 3
1 2
4 1
96 4
1 96
2 1
4 2
2 4
1 2
5 1
95 5
1 95
2 1
5 2
2 5
1 2
6 1
94 6
1 94
2 1
6 2
2 6
1 2
7 1
93 7
1 93
2 1
7 2
2 7
1 2
8 1
92 8
1 92
2 1
8 2
2 8
1 2
9 1
91 9
1 91
2 1
9 2
2 9
1 2
10 1
90 10
1 90
2 1
10 2
2 10
1 2
11 1
89 11
1 89
2 1
...

result:

ok correct plan!

Test #10:

score: 0
Accepted
time: 1ms
memory: 3724kb

input:

100 3
1 0
100 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 ...

output:

2 1
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
...

result:

ok correct plan!

Test #11:

score: 0
Accepted
time: 0ms
memory: 3600kb

input:

100 4
43 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
39 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
26 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...

output:

1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
3 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
...

result:

ok correct plan!

Test #12:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

6 3
4 2 3 0 0
3 4 1 6
3 5 0 0

output:

1 3
1 3
2 1
3 1
3 1
3 2
1 3
1 3
1 3
3 1
2 3
2 1
2 1
3 2
1 2
1 2
1 3
2 1
3 2
1 3
3 1
2 3
2 1
3 2
1 2
1 3
2 1
3 1
3 1
3 1
1 3
1 3
1 2
1 3
2 1
3 1
3 1
3 1
1 3
1 2
1 3
2 1
3 1
3 1
1 2
1 3
2 1
3 1
1 2
2 1
0 0

result:

ok correct plan!