QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116104#5280. Depot Rearrangementxtqqwq#100 ✓22ms20140kbC++142.5kb2023-06-28 09:41:462024-05-31 14:20:37

Judging History

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

  • [2024-05-31 14:20:37]
  • 评测
  • 测评结果:100
  • 用时:22ms
  • 内存:20140kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-28 09:41:46]
  • 提交

answer

#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;

template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}

int readint(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

int n,m,tot,cnt;
int v[160005],c[160005],nxt[160005],h[405],ans[160005],a[160005],num[405][405],f[405],fr[405][405];
bool vis[160005];
vector<int> hv[405][405];
vector<pii> res;

void addedge(int x,int y,int z){
	v[++tot]=y; c[tot]=z; nxt[tot]=h[x]; h[x]=tot;
}

int getf(int x){return x==f[x]?x:f[x]=getf(f[x]);}

void merge(int x,int y){
	int fx=getf(x),fy=getf(y);
	if(fx==fy) return;
	f[fx]=fy;
}

void dfs(int u){
	for(int &p=h[u];p;p=nxt[p]){
		if(vis[p]) continue;
		vis[p]=1;
		int edge=p;
		dfs(v[p]);
		ans[++cnt]=edge;
	}
}

int main(){
	n=readint(); m=readint();
	for(int i=1;i<=n*m;i++) a[i]=readint();
	for(int i=1;i<=n;i++){
		for(int j=(i-1)*m+1;j<=i*m;j++) num[i][a[j]]++;
		for(int j=(i-1)*m+1;j<=i*m;j++) hv[i][a[j]].pb(j);
	}
	for(int i=1;i<=m;i++){
		vector<int> vec;
		for(int j=1;j<=n;j++) if(!num[j][i]) vec.pb(j);
		for(int j=1;j<=n;j++){
			for(int k=2;k<=num[j][i];k++){
				addedge(j,vec.back(),i);
				fr[i][vec.back()]=tot;
				vec.pop_back();
			}
		}
	}
	for(int i=1;i<=n;i++) f[i]=i;
	for(int i=1;i<=n;i++) for(int p=h[i];p;p=nxt[p]) merge(i,v[p]);
	for(int i=1;i<=m;i++){
		vector<int> vec;
		for(int j=1;j<=n;j++) if(!num[j][i]) vec.pb(j);
		for(int j=1;j<vec.size();j++){
			if(getf(vec[j])!=getf(vec[0])){
				int t1=fr[i][vec[0]],t2=fr[i][vec[j]];
				swap(v[t1],v[t2]);
				merge(vec[0],vec[j]);
			}
		}
	}
	for(int i=1;i<=n;i++){
		cnt=0;
		dfs(i);
		if(!cnt) continue;
		// cout<<"##### "<<endl;
		// for(int j=cnt;j>=1;j--) cout<<v[ans[j]]<<' ';
		// cout<<endl;
		int lst=hv[i][c[ans[cnt]]].back();
		hv[i][c[ans[cnt]]].pop_back();
		res.pb(mp(lst,n*m+1));
		for(int i=1;i<cnt;i++){
			int tmp=hv[v[ans[i+1]]][c[ans[i]]].back();
			hv[v[ans[i+1]]][c[ans[i]]].pop_back();
			res.pb(mp(tmp,lst));
			lst=tmp;
		}
		res.pb(mp(n*m+1,lst));
	}
	printf("%d\n",res.size());
	for(auto r:res) printf("%d %d\n",r.fi,r.se);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 0ms
memory: 10732kb

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: 0ms
memory: 9772kb

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
4 21
10 4
20 10
8 20
15 8
7 15
19 7
3 19
14 3
2 14
18 2
12 18
21 12

result:

ok both subtasks are correct!

Test #3:

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

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
3 101
55 3
39 55
76 39
49 76
38 49
97 38
18 97
87 18
29 87
90 29
30 90
100 30
43 100
20 43
95 20
28 95
67 28
56 67
50 56
27 50
75 27
36 75
44 36
79 44
16 79
26 16
58 26
66 58
6 66
89 6
101 89

result:

ok both subtasks are correct!

Test #4:

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

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
109 1001
453 109
713 453
819 713
210 819
850 210
734 850
184 734
814 184
359 814
775 359
670 775
830 670
175 830
949 175
1001 949
707 1001
747 707
1001 747

result:

ok both subtasks are correct!

Test #5:

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

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
273 20001
16173 273
16836 16173
7854 16836
10354 7854
3158 10354
15979 3158
6579 15979
17199 6579
8977 17199
5861 8977
11861 5861
1862 11861
17855 1862
1268 17855
19768 1268
4647 19768
12213 4647
3013 12213
9473 3013
8323 9473
9423 8323
13073 9423
19047 13073
6247 19047
19747 6247
459 19747
1541...

result:

ok both subtasks are correct!

Test #6:

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

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
20 4021
3912 20
140 3912
3800 140
279 3800
3379 279
1339 3379
2600 1339
2165 2600
1351 2165
2840 1351
988 2840
2828 988
1365 2828
2391 1365
1375 2391
2580 1375
2196 2580
1940 2196
2000 1940
1656 2000
2416 1656
1898 2416
2314 1898
1699 2314
2271 1699
1833 2271
1971 1833
2617 1971
1578 2617
2768 ...

result:

ok both subtasks are correct!

Test #7:

score: 5
Accepted
time: 0ms
memory: 14192kb

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
1262 90001
72662 1262
11820 72662
72720 11820
75733 72720
37143 75733
87877 37143
34177 87877
39694 34177
34894 39694
8377 34894
84877 8377
35684 84877
60545 35684
87208 60545
60508 87208
35645 60508
84884 35645
16913 84884
88791 16913
5865 88791
40307 5865
69710 40307
15410 69710
49695 15410
15...

result:

ok both subtasks are correct!

Test #8:

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

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
40 12041
602 40
6020 602
2000 6020
3600 2000
1160 3600
9030 1160
6440 9030
7400 6440
8880 7400
120 8880
1204 120
9632 1204
6120 9632
11320 6120
360 11320
9331 360
3120 9331
12000 3120
11480 12000
1560 11480
12040 1560
5760 12040
8320 5760
7360 8320
8280 7360
9760 8280
400 9760
3612 400
9880 36...

result:

ok both subtasks are correct!

Test #9:

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

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
95 40001
39929 95
600 39929
38955 600
1465 38955
38385 1465
1181 38385
39582 1181
970 39582
38764 970
793 38764
39317 793
940 39317
37651 940
3105 37651
37279 3105
3672 37279
36775 3672
1222 36775
38516 1222
2499 38516
37386 2499
3400 37386
36892 3400
2687 36892
37174 2687
3773 37174
36625 377...

result:

ok both subtasks are correct!

Test #10:

score: 5
Accepted
time: 0ms
memory: 9884kb

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
154 6401
6047 154
750 6047
4827 750
806 4827
5152 806
1804 5152
3066 1804
5714 3066
2805 5714
4819 2805
2906 4819
4098 2906
2238 4098
3835 2238
977 3835
6226 977
182 6226
6351 182
1034 6351
4702 1034
1268 4702
4455 1268
2105 4455
4345 2105
2009 4345
5638 2009
2065 5638
2622 2065
1280 2622
4894 ...

result:

ok both subtasks are correct!

Test #11:

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

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
99 40001
39896 99
763 39896
39251 763
1825 39251
38687 1825
2347 38687
37418 2347
2463 37418
38386 2463
866 38386
39582 866
397 39582
38477 397
2583 38477
38096 2583
1265 38096
38363 1265
2548 38363
38335 2548
2159 38335
37994 2159
2545 37994
37197 2545
2889 37197
36773 2889
2999 36773
36152 2...

result:

ok both subtasks are correct!

Test #12:

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

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
19 6021
5603 19
40 5603
4987 40
980 4987
3579 980
2860 3579
2299 2860
820 2299
1800 820
560 1800
1200 560
1780 1200
559 1780
1860 559
1759 1860
4140 1759
1020 4140
2080 1020
5019 2080
79 5019
4840 79
380 4840
4648 380
3999 4648
3740 3999
740 3740
5700 740
39 5700
4637 39
100 4637
3219 100
2880 ...

result:

ok both subtasks are correct!

Test #13:

score: 5
Accepted
time: 9ms
memory: 14976kb

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
187 90001
88718 187
3228 88718
89375 3228
861 89375
89903 861
1710 89903
89458 1710
597 89458
89586 597
3698 89586
89448 3698
3809 89448
86989 3809
1962 86989
89945 1962
3862 89945
87878 3862
4975 87878
86343 4975
5664 86343
86061 5664
5645 86061
85786 5645
5432 85786
83877 5432
13026 83877
79...

result:

ok both subtasks are correct!

Test #14:

score: 5
Accepted
time: 11ms
memory: 16652kb

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
379 80401
80175 379
201 80175
79649 201
200 79649
79428 200
199 79428
78772 199
197 78772
77905 197
196 77905
80397 196
689 80397
77586 689
195 77586
76659 195
193 76659
23943 193
10505 23943
5226 10505
1608 5226
7867 1608
3216 7867
1206 3216
19945 1206
11658 19945
6080 11658
2466 6080
4980 24...

result:

ok both subtasks are correct!

Test #15:

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

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
784 160001
118784 784
160001 118784
3172 160001
11722 3172
2922 11722
147572 2922
133420 147572
87336 133420
132109 87336
90155 132109
48288 90155
83888 48288
83273 83888
90073 83273
8955 90073
25053 8955
75857 25053
25057 75857
138790 25057
78790 138790
158790 78790
25190 158790
16377 25190
205...

result:

ok both subtasks are correct!

Test #16:

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

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
174 60201
56932 174
4199 56932
48162 4199
19395 48162
39524 19395
15960 39524
30737 15960
30595 30737
20336 30595
34180 20336
23582 34180
38585 23582
18794 38585
35563 18794
20046 35563
34774 20046
21923 34774
32777 21923
23924 32777
25708 23924
30186 25708
20918 30186
27912 20918
28369 27912
...

result:

ok both subtasks are correct!

Test #17:

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

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
400 80401
80204 400
201 80204
79790 201
200 79790
77466 200
79394 77466
199 79394
57854 199
29346 57854
55677 29346
68943 55677
74973 68943
78993 74973
198 78993
69583 198
75375 69583
78591 75375
197 78591
53163 197
66933 53163
33366 66933
16482 33366
8040 16482
45224 8040
22676 45224
51657 22...

result:

ok both subtasks are correct!

Test #18:

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

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
298 120001
119841 298
427 119841
117447 427
4961 117447
117741 4961
3291 117741
116023 3291
3874 116023
115195 3874
7699 115195
112474 7699
10349 112474
104068 10349
20429 104068
99395 20429
18771 99395
102592 18771
22938 102592
92092 22938
21574 92092
93235 21574
33854 93235
84548 33854
40410...

result:

ok both subtasks are correct!

Test #19:

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

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
281 132868
132851 281
333 132851
132131 333
332 132131
132021 332
330 132021
102465 330
131535 102465
329 131535
131139 329
327 131139
25812 327
130536 25812
325 130536
105265 325
748 105265
129882 748
324 129882
129599 324
323 129599
128986 323
322 128986
104817 322
666 104817
128754 666
321...

result:

ok both subtasks are correct!

Test #20:

score: 5
Accepted
time: 22ms
memory: 17628kb

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
397 160001
159531 397
569 159531
159956 569
523 159956
159431 523
546 159431
156596 546
6957 156596
151700 6957
6820 151700
153436 6820
5423 153436
149013 5423
7596 149013
149198 7596
3876 149198
151144 3876
15034 151144
139974 15034
15198 139974
142387 15198
11751 142387
142706 11751
11990 14...

result:

ok both subtasks are correct!