QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#117376#5280. Depot Rearrangementkshitij_sodani100 ✓95ms19276kbC++142.7kb2023-07-01 02:37:592023-07-01 02:39:28

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-01 02:39:28]
  • 评测
  • 测评结果:100
  • 用时:95ms
  • 内存:19276kb
  • [2023-07-01 02:37:59]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
//#define endl '\n'
int n,m;
int it[401][401];
vector<int> pre[401][401];
int co[401][401];
int tt[401*401];
vector<pair<int,int>> adj[801];
int vis[801];
vector<pair<int,int>> xx[801];
vector<pair<int,int>> cot2;
void dfs(int no){
	vector<pair<int,int>> zz=xx[no];
	xx[no].clear();
	for(auto j:zz){
		if(xx[j.a].size()){
			dfs(j.a);
		}
		cot2.pb(j);
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>it[i][j];
			tt[i*m+j]=it[i][j];
			it[i][j]--;
			pre[i][it[i][j]].pb(j);
			co[i][it[i][j]]++;
		}
		for(int j=0;j<m;j++){
			co[i][j]-=1;
			if(co[i][j]==-1){
				adj[j+n].pb({i,-1});
			}
			else if(co[i][j]>0){
				for(int k=0;k+1<pre[i][j].size();k++){
					adj[i].pb({j+n,pre[i][j][k]+i*m});
				}
			}
		}
	}
	vector<pair<int,int>> ans;
	for(int i=0;i<n;i++){
		if(adj[i].size()){
			cot2.clear();
			int cur=i;
			vector<pair<int,int>> cot;
			while(true){
				if(cur==i and adj[cur].size()==0){
					break;
				}
				cot.pb({cur,adj[cur].back().b});
				int ne=adj[cur].back().a;
				adj[cur].pop_back();
				cot.pb({ne,adj[ne].back().b});
				cur=adj[ne].back().a;
				adj[ne].pop_back();
			}
			vector<int> yy;
			for(int i=0;i<cot.size();i++){
				yy.pb(cot[i].a);
				vis[cot[i].a]=1;
			}
			while(yy.size()){
				vector<int> zz;
				for(int i=0;i<yy.size();i++){
					if(adj[yy[i]].size()){
						cur=yy[i];
						while(true){
							if(cur==yy[i] and adj[cur].size()==0){
								break;
							}
							if(vis[cur]==0){
								vis[cur]=1;
								zz.pb(cur);
							}
							xx[yy[i]].pb({cur,adj[cur].back().b});
							
					//		cot.pb({cur,adj[cur].back().b})
							int ne=adj[cur].back().a;
							adj[cur].pop_back();
							if(vis[ne]==0){
								vis[ne]=1;
								zz.pb(ne);

							}
							xx[yy[i]].pb({ne,adj[ne].back().b});
							cur=adj[ne].back().a;
							adj[ne].pop_back();
						}
					}
				}
				for(auto j:zz){
					vis[j]=1;
				}
				yy=zz;
			}
			
			for(auto j:cot){
				dfs(j.a);
				cot2.pb(j);
			}
			cot.clear();
			for(int j=0;j<cot2.size();j+=2){
				cot.pb(cot2[j]);
			}
			for(auto j:cot){
				assert(adj[j.a].size()==0);
			}

			ans.pb({cot[0].b,n*m});
			for(int i=cot.size()-1;i>=1;i--){
				ans.pb({cot[i].b,cot[(i+1)%((int)cot.size())].b});
			}
			ans.pb({n*m,cot[1].b});
		}
	}
	cout<<ans.size()<<endl;
	for(auto j:ans){
		tt[j.b]=tt[j.a];
		cout<<j.a+1<<" "<<j.b+1<<endl;
	}
/*	for(int j=0;j<n*m;j++){
		cout<<tt[j]<<",";
	}
	cout<<endl;
*/








	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 1ms
memory: 7324kb

input:

10 3
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3


output:

0

result:

ok both subtasks are correct!

Test #2:

score: 5
Accepted
time: 2ms
memory: 7308kb

input:

5 4
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4


output:

13
3 21
9 3
1 9
13 1
6 13
14 6
2 14
17 2
7 17
18 7
11 18
19 11
21 19

result:

ok both subtasks are correct!

Test #3:

score: 5
Accepted
time: 2ms
memory: 7628kb

input:

10 10
8 10 10 3 7 3 5 6 1 4 3 8 2 9 1 8 4 2 7 3 10 7 9 2 1 10 10 9 1 2 9 7 4 5 2 9 10 5 7 6 6 8 6 8 4 2 9 1 2 8 6 1 4 2 2 1 5 6 3 10 10 7 9 4 8 9 8 2 5 6 4 3 1 6 3 3 10 7 7 5 3 6 8 5 9 4 6 7 9 4 10 5 3 4 5 1 1 7 8 5


output:

32
2 101
13 2
34 13
25 34
72 25
24 72
86 24
46 86
32 46
11 32
41 11
4 41
23 4
65 23
52 65
63 52
75 63
31 75
12 31
21 12
82 21
96 82
54 96
42 54
26 42
92 26
51 92
78 51
44 78
95 44
85 95
101 85

result:

ok both subtasks are correct!

Test #4:

score: 5
Accepted
time: 2ms
memory: 8356kb

input:

100 10
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 9 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10...

output:

19
101 1001
451 101
183 451
733 183
711 733
811 711
352 811
202 352
825 202
665 825
844 665
812 844
772 812
172 772
942 172
1001 942
706 1001
746 706
1001 746

result:

ok both subtasks are correct!

Test #5:

score: 5
Accepted
time: 3ms
memory: 8536kb

input:

200 100
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 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

output:

195
227 20001
13010 227
2210 13010
5587 2210
11487 5587
15341 11487
6541 15341
8551 6541
9111 8551
10172 9111
12816 10172
18216 12816
4415 18216
18215 4415
10572 18215
16490 10572
13390 16490
8511 13390
6059 8511
3716 6059
4216 3716
5833 4216
11849 5833
12449 11849
8759 12449
8559 8759
8051 8559
159...

result:

ok both subtasks are correct!

Test #6:

score: 5
Accepted
time: 5ms
memory: 8772kb

input:

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

output:

1401
19 4021
83 19
42 83
35 42
72 35
85 72
249 85
25 249
3 25
48 3
166 48
203 166
101 203
328 101
104 328
356 104
22 356
7 22
26 7
9 26
138 9
143 138
415 143
93 415
169 93
65 169
184 65
367 184
82 367
73 82
97 73
498 97
46 498
23 46
167 23
144 167
123 144
45 123
308 45
354 308
431 354
124 431
187 12...

result:

ok both subtasks are correct!

Test #7:

score: 5
Accepted
time: 7ms
memory: 11440kb

input:

300 300
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 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

output:

205
1227 90001
72627 1227
11765 72627
72665 11765
75609 72665
36905 75609
68105 36905
22592 68105
36352 22592
38452 36352
39415 38452
38515 39415
15003 38515
80403 15003
65716 80403
58284 65716
3130 58284
77830 3130
20226 77830
21726 20226
65784 21726
16516 65784
26318 16516
16718 26318
30072 16718
...

result:

ok both subtasks are correct!

Test #8:

score: 5
Accepted
time: 13ms
memory: 9120kb

input:

301 40
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

11624
39 12041
321 39
41 321
361 41
81 361
401 81
121 401
441 121
161 441
481 161
201 481
521 201
241 521
561 241
1 561
641 1
601 641
42 601
642 42
82 642
681 82
122 681
721 122
162 721
761 162
202 761
801 202
242 801
841 242
281 841
682 281
322 682
722 322
362 722
762 362
402 762
802 402
442 802
84...

result:

ok both subtasks are correct!

Test #9:

score: 5
Accepted
time: 6ms
memory: 10308kb

input:

400 100
11 65 1 79 15 18 79 46 9 30 71 53 58 55 94 73 39 16 6 91 49 30 23 30 28 81 90 48 97 54 79 30 94 18 42 77 44 36 5 48 55 97 79 36 41 59 79 71 32 59 3 10 63 52 44 41 9 46 31 31 56 87 60 80 12 51 15 78 41 65 95 34 29 83 46 64 37 53 98 17 41 45 36 73 20 53 48 80 57 54 57 72 39 56 98 6 10 78 11 72...

output:

14592
79 40001
149 79
220 149
153 220
19 153
484 19
731 484
1118 731
1083 1118
242 1083
1346 242
327 1346
9 327
550 9
410 550
52 410
422 52
1622 422
337 1622
112 337
1518 112
637 1518
1770 637
2152 1770
743 2152
1932 743
567 1932
135 567
2238 135
345 2238
2934 345
2618 2934
761 2618
223 761
141 223
...

result:

ok both subtasks are correct!

Test #10:

score: 5
Accepted
time: 4ms
memory: 8432kb

input:

40 160
17 2 3 4 5 6 7 91 9 10 154 12 103 14 15 16 17 25 19 58 21 8 23 24 52 26 27 58 120 105 50 55 104 32 35 36 37 38 45 10 41 42 43 44 45 71 47 48 49 34 140 52 53 54 115 44 28 58 59 60 61 62 63 64 132 66 67 68 69 70 71 69 24 74 75 76 77 133 79 80 81 82 100 84 31 86 87 88 100 90 91 92 93 94 95 96 97...

output:

1316
11 6401
331 11
9 331
1921 9
966 1921
803 966
175 803
10 175
351 10
176 351
805 176
987 805
481 987
644 481
988 644
1286 988
2088 1286
1 2088
702 1
185 702
361 185
991 361
807 991
186 807
24 186
190 24
2722 190
1606 2722
655 1606
2242 655
1444 2242
1771 1444
1448 1771
483 1448
3210 483
2401 3210...

result:

ok both subtasks are correct!

Test #11:

score: 5
Accepted
time: 17ms
memory: 10232kb

input:

400 100
88 82 9 2 90 1 83 32 32 79 8 79 63 67 85 82 50 63 69 2 7 91 21 90 69 3 39 78 66 83 96 53 24 65 56 63 90 54 35 55 94 22 76 12 54 55 5 49 91 73 8 19 64 54 39 23 13 27 34 4 81 52 13 11 36 45 3 50 82 81 42 50 75 15 99 70 29 26 70 66 34 15 42 83 16 19 19 12 76 1 68 49 7 17 64 37 98 34 99 37 34 64...

output:

14611
75 40001
432 75
1346 432
550 1346
1026 550
501 1026
334 501
284 334
1803 284
816 1803
1615 816
858 1615
353 858
590 353
293 590
1831 293
1918 1831
1248 1918
374 1248
221 374
2110 221
2030 2110
6 2030
307 6
103 307
319 103
2209 319
4 2209
308 4
239 308
529 239
1242 529
1188 1242
354 1188
532 35...

result:

ok both subtasks are correct!

Test #12:

score: 5
Accepted
time: 3ms
memory: 8488kb

input:

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

output:

4260
10 6021
321 10
22 321
342 22
41 342
361 41
62 361
382 62
81 382
401 81
121 401
421 121
142 421
441 142
161 441
481 161
202 481
501 202
241 501
523 241
261 523
542 261
282 542
601 282
2 601
621 2
23 621
641 23
42 641
661 42
63 661
722 63
82 722
741 82
101 741
762 101
123 762
783 123
143 783
801 ...

result:

ok both subtasks are correct!

Test #13:

score: 5
Accepted
time: 42ms
memory: 12652kb

input:

300 300
215 159 263 206 201 183 286 56 142 10 231 214 34 54 263 250 169 208 239 148 104 22 244 17 74 68 184 52 2 30 42 83 222 106 25 152 37 225 213 213 69 273 91 221 207 48 166 28 221 50 46 64 10 254 207 109 206 144 270 291 195 197 253 235 141 186 102 68 52 24 38 6 181 44 256 200 77 233 285 163 223 ...

output:

32648
171 90001
1514 171
302 1514
1887 302
618 1887
2507 618
712 2507
72 712
1549 72
3017 1549
152 3017
1804 152
3029 1804
931 3029
5404 931
974 5404
2424 974
6090 2424
1376 6090
10 1376
1223 10
393 1223
657 393
1590 657
53 1590
7582 53
3426 7582
127 3426
614 127
467 614
942 467
1226 942
1539 1226
1...

result:

ok both subtasks are correct!

Test #14:

score: 5
Accepted
time: 23ms
memory: 13748kb

input:

201 400
1 1 1 1 1 152 1 1 1 1 1 1 1 33 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 300 154 1 1 147 1 1 1 383 186 1 1 90 256 1 1 1 1 1 1 1 63 1 1 1 1 208 1 1 1 1 31 1 1 1 1 1 1 1 127 1 1 29 216 397 393 1 1 1 1 1 1 279 1 1 1 1 55 1 1 215 249 1 1 1 1 1 1 172 1 1 1 1 1 1 1 1 1 1 1 1 349 1 331 1 1 1 1 1 1 1 34...

output:

63990
369 80401
403 369
1 403
801 1
2 801
1601 2
1201 1601
3 1201
2001 3
1203 2001
802 1203
404 802
1602 404
803 1602
1603 803
4 1603
2401 4
1204 2401
405 1204
2002 405
804 2002
2003 804
5 2003
3201 5
1205 3201
2004 1205
1604 2004
406 1604
2402 406
7 2402
806 7
2404 806
2005 2404
408 2005
809 408
28...

result:

ok both subtasks are correct!

Test #15:

score: 5
Accepted
time: 10ms
memory: 14216kb

input:

400 400
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 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

output:

217
632 160001
118632 632
160001 118632
3020 160001
11717 3020
2917 11717
147420 2917
133336 147420
87309 133336
132079 87309
90125 132079
83267 90125
90067 83267
8925 90067
24903 8925
139791 24903
110991 139791
75791 110991
24991 75791
88529 24991
62529 88529
115888 62529
83673 115888
48073 83673
6...

result:

ok both subtasks are correct!

Test #16:

score: 5
Accepted
time: 15ms
memory: 11332kb

input:

301 200
50 129 146 60 183 51 47 77 26 73 1 45 1 44 149 1 81 196 17 16 163 35 159 71 1 94 161 138 138 27 76 1 102 42 5 186 176 1 111 198 37 63 81 155 95 164 132 135 155 194 126 98 31 34 121 19 175 148 33 105 25 122 91 165 1 69 1 197 12 98 1 155 5 53 42 1 60 98 78 61 155 13 1 171 102 152 95 61 87 200 ...

output:

23506
111 60201
2818 111
432 2818
1234 432
402 1234
1651 402
1890 1651
1253 1890
2277 1253
3347 2277
11 3347
1808 11
621 1808
4848 621
2027 4848
5205 2027
13 5205
2434 13
419 2434
6214 419
2689 6214
458 2689
3022 458
467 3022
2906 467
633 2906
3027 633
6408 3027
232 6408
6910 232
3881 6910
7079 3881...

result:

ok both subtasks are correct!

Test #17:

score: 5
Accepted
time: 36ms
memory: 15452kb

input:

201 400
1 1 1 1 1 1 1 1 1 1 1 1 1 263 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 246 1 1 1 1 1 1 1 1 1 1 1 1 1 1 107 1 1 1 1 1 1 1 1 57 1 1 1 1 1 1 1 224 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 90 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

77869
399 80401
801 399
1 801
1201 1
802 1201
401 802
1202 401
2 1202
1603 2
803 1603
403 803
1604 403
3 1604
2001 3
804 2001
1605 804
1203 1605
404 1203
2002 404
4 2002
2401 4
5 2401
2801 5
6 2801
3201 6
7 3201
3601 7
8 3601
4001 8
9 4001
4401 9
10 4401
4801 10
11 4801
5201 11
12 5201
805 12
2003 8...

result:

ok both subtasks are correct!

Test #18:

score: 5
Accepted
time: 48ms
memory: 14652kb

input:

400 300
75 26 289 176 131 196 124 8 230 157 247 265 13 2 210 141 17 200 187 83 21 22 118 144 232 26 284 75 48 30 132 32 65 34 72 36 73 286 164 40 41 261 65 270 221 12 139 48 49 143 91 39 17 258 275 56 151 194 282 55 228 266 296 64 22 232 67 142 69 152 10 102 109 45 75 49 283 112 78 283 81 236 169 22...

output:

43105
203 120001
304 203
1505 304
392 1505
685 392
355 685
3007 355
1204 3007
14 1204
692 14
4063 692
604 4063
3909 604
1206 3909
594 1206
4503 594
745 4503
1512 745
1208 1512
223 1208
2438 223
1216 2438
310 1216
2101 310
5115 2101
3706 5115
5402 3706
4201 5402
5553 4201
4508 5553
161 4508
2186 161
...

result:

ok both subtasks are correct!

Test #19:

score: 5
Accepted
time: 95ms
memory: 19276kb

input:

333 399
1 1 1 1 1 1 1 28 1 1 1 1 1 1 161 1 17 1 1 1 1 262 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 43 1 1 1 1 1 70 1 1 1 142 1 1 1 1 1 1 1 1 1 1 1 1 70 1 1 1 1 1 1 278 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 245 1 1 1 1 1 1 33 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 106 1 1 1 1 268 1 1 1 172 1 1 1 1 1 312 1 286 1 1 1 1 ...

output:

114795
223 132868
1198 223
799 1198
400 799
1199 400
401 1199
1 401
1597 1
404 1597
1598 404
800 1598
1599 800
1200 1599
2 1200
1996 2
405 1996
1997 405
801 1997
1998 801
1600 1998
3 1600
1999 3
4 1999
2395 4
406 2395
2000 406
1201 2000
2865 1201
407 2865
2397 407
802 2397
2398 802
1202 2398
2399 12...

result:

ok both subtasks are correct!

Test #20:

score: 5
Accepted
time: 66ms
memory: 16728kb

input:

400 400
100 35 353 385 317 228 7 148 113 165 11 306 209 89 21 166 17 2 19 249 27 305 377 22 3 353 38 28 29 96 191 32 33 309 35 308 100 176 152 40 176 42 43 86 45 46 96 48 396 381 218 246 53 54 334 159 243 360 294 60 33 62 185 64 65 66 191 121 351 107 10 343 367 74 75 201 77 247 79 134 304 92 42 126 ...

output:

55816
270 160001
2001 270
523 2001
18 523
406 18
1642 406
4227 1642
801 4227
1481 801
1606 1481
2007 1606
5201 2007
2401 5201
11202 2401
2801 11202
802 2801
473 802
813 473
11692 813
3817 11692
13311 3817
4609 13311
147 4609
2813 147
2008 2813
1611 2008
14003 1611
2632 14003
15923 2632
4801 15923
25...

result:

ok both subtasks are correct!