QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116155#5280. Depot Rearrangementyoungsystem#100 ✓29ms28400kbC++202.6kb2023-06-28 11:04:592024-05-31 18:18:47

Judging History

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

  • [2024-05-31 18:18:47]
  • 评测
  • 测评结果:100
  • 用时:29ms
  • 内存:28400kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-28 11:04:59]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		n=n*10+ch-'0';
		ch=getchar();
	}
	return n*f;
}
int p[200005];
int sl[200005];
bool cz[200005],vis[200005];
vector<int>v[200005];
vector<int>yy[200005],qs[200005];
int fa[200005];
int findf(int n)
{
	if(fa[n]==n)return n;
	return fa[n]=findf(fa[n]);
}
int xl1[200005],cnt1;
int xl2[200005],cnt2;
bool visd[200005];
int n,m;
vector<int>wz[200005];
int now[1000005];
int pp[1000005];
int sta[1000005],ttop;
void dfs1(int x)
{
	for(int i=now[x];i<v[x].size();i=now[x])
	{
		now[x]=i+1;
		dfs1(v[x][i]);
	}
	sta[++ttop]=x;
}
int tsl[405][405];
int sc1[1000005],sc2[1000005];
int xl[1000005],cnt;
int main()
{
	n=read();
	m=read();
	for(int i=1;i<=n*m;i++)
	{
		p[i]=read();
		pp[i]=p[i];
	}
	for(int i=1;i<=m;i++)fa[i]=i;
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)sl[j]=0;
		for(int j=1;j<=m;j++)
		{
			sl[p[(i-1)*m+j]]++;
			if(sl[p[(i-1)*m+j]]>=2)
			{
				cz[p[(i-1)*m+j]]=true;
				qs[i].push_back(p[(i-1)*m+j]);
				wz[p[(i-1)*m+j]].push_back((i-1)*m+j);
			}
		} 
		for(int j=1;j<=m;j++)if(sl[j]==0)yy[i].push_back(j);
		for(int j=0;j<yy[i].size();j++)
		{
			v[yy[i][j]].push_back(i+m);
			v[i+m].push_back(qs[i][j]);
		}
		if(!yy[i].empty())
		{
			for(int j=1;j<yy[i].size();j++)fa[findf(yy[i][0])]=findf(yy[i][j]);
			for(int j=0;j<qs[i].size();j++)fa[findf(yy[i][0])]=findf(qs[i][j]);
		}
	}
	for(int i=1;i<=m;i++)
	{
		if(!cz[i])continue;
		if(findf(i)==i)
		{
			v[m+n+1].push_back(i);
			v[i].push_back(m+n+1);
		}
	}
	dfs1(m+n+1);
	reverse(sta+1,sta+ttop+1);
	cnt1=0;
	for(int i=ttop;i>=1;i--)
	{
		if(sta[i]==n+m+1)
		{
			xl[++cnt]=n*m+1;
		}
		else if(sta[i]>m)
		{
			continue;
		}
		else
		{
			if(sta[i-1]==n+m+1)continue;
			int bh=sta[i-1]-m;
			for(int j=1;j<=m;j++)
			{
				if(p[(bh-1)*m+j]==sta[i])
				{
					xl[++cnt]=(bh-1)*m+j;
					p[(bh-1)*m+j]=-1;
					break;
				}
			}
		}
	}
	printf("%d\n",cnt-1);
	for(int i=1;i<=cnt-1;i++)printf("%d %d\n",xl[i+1],xl[i]);
	return 0;
	for(int i=1;i<=n*m;i++)p[i]=pp[i];
	for(int i=1;i<=cnt-1;i++)
	{
		int now1=xl[i+1],now2=xl[i];
		assert(p[now2]==0);
		p[now2]=p[now1];
		p[now1]=0;
	}
	for(int i=1;i<=n*m;i++)printf("%d ",p[i]);
	printf("\n");
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)sl[j]=0;
		for(int j=1;j<=m;j++)sl[p[(i-1)*m+j]]++;
		for(int j=1;j<=m;j++)assert(sl[j]==1);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 11980kb

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
1 21
17 1
11 17
18 11
6 18
13 6
7 13
19 7
2 19
14 2
3 14
9 3
21 9

result:

ok both subtasks are correct!

Test #3:

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

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

result:

ok both subtasks are correct!

Test #4:

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

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
352 1001
202 352
825 202
665 825
844 665
733 844
711 733
811 711
772 811
172 772
942 172
101 942
451 101
183 451
812 183
1001 812
706 1001
746 706
1001 746

result:

ok both subtasks are correct!

Test #5:

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

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
1145 20001
9245 1145
20001 9245
19632 20001
4443 19632
4858 4443
4458 4858
18934 4458
17217 18934
16301 17217
18908 16301
7034 18908
17734 7034
9974 17734
18894 9974
17984 18894
12919 17984
18471 12919
14213 18471
9005 14213
8457 9005
12816 8457
18216 12816
10572 18216
16490 10572
17325 16490
62...

result:

ok both subtasks are correct!

Test #6:

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

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
3916 4021
3942 3916
3906 3942
3990 3906
3970 3990
3938 3970
3904 3938
3983 3904
4004 3983
3799 4004
3973 3799
4009 3973
3743 4009
3978 3743
4007 3978
3943 4007
3850 3943
3902 3850
3648 3902
3922 3648
3984 3922
3921 3984
3911 3921
3988 3911
4002 3988
3875 4002
3618 3875
4001 3618
3853 4001
3889 ...

result:

ok both subtasks are correct!

Test #7:

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

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
28480 90001
57280 28480
90001 57280
45771 90001
47571 45771
90001 47571
59425 90001
83425 59425
90001 83425
60353 90001
35636 60353
84804 35636
16877 84804
76131 16877
18628 76131
74232 18628
60132 74232
73079 60132
18779 73079
25954 18779
18754 25954
75928 18754
21999 75928
80799 21999
82984 80...

result:

ok both subtasks are correct!

Test #8:

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

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
241 12041
12001 241
11681 12001
11961 11681
11641 11961
11921 11641
11601 11921
11881 11601
11561 11881
11841 11561
11521 11841
11801 11521
11481 11801
11761 11481
11441 11761
12002 11441
11361 12002
11439 11361
11962 11439
11362 11962
11922 11362
11321 11922
11882 11321
11281 11882
11842 1128...

result:

ok both subtasks are correct!

Test #9:

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

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
39472 40001
39708 39472
39972 39708
39548 39972
39951 39548
39611 39951
39770 39611
39953 39770
39851 39953
39916 39851
39811 39916
39925 39811
39884 39925
39519 39884
39797 39519
39601 39797
39719 39601
39647 39719
39959 39647
39871 39959
39348 39871
39702 39348
38904 39702
39711 38904
39950 ...

result:

ok both subtasks are correct!

Test #10:

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

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
3562 6401
5924 3562
4249 5924
6114 4249
6313 6114
5433 6313
4570 5433
5026 4570
6306 5026
5315 6306
5462 5315
5849 5462
4146 5849
5435 4146
6067 5435
5329 6067
6353 5329
4732 6353
5597 4732
6109 5597
4630 6109
4997 4630
6015 4997
6094 6015
4008 6094
5824 4008
6305 5824
6149 6305
5732 6149
6021 ...

result:

ok both subtasks are correct!

Test #11:

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

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
39576 40001
39694 39576
39812 39694
39794 39812
39818 39794
39404 39818
39999 39404
39822 39999
39990 39822
39672 39990
39988 39672
39742 39988
39128 39742
39805 39128
39618 39805
39403 39618
39532 39403
39821 39532
39705 39821
39802 39705
39608 39802
39503 39608
39713 39503
39843 39713
39602 ...

result:

ok both subtasks are correct!

Test #12:

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

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
4824 6021
6001 4824
5681 6001
5961 5681
5661 5961
5941 5661
5641 5941
6002 5641
5935 6002
5621 5935
5901 5621
5602 5901
5881 5602
5582 5881
5861 5582
5561 5861
5802 5561
5541 5802
5781 5541
5521 5781
5741 5521
5461 5741
6003 5461
5381 6003
5421 5381
5981 5421
5361 5981
5419 5361
5962 5419
5382 ...

result:

ok both subtasks are correct!

Test #13:

score: 5
Accepted
time: 8ms
memory: 22236kb

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
88885 90001
89584 88885
89781 89584
88223 89781
89456 88223
89785 89456
88546 89785
89559 88546
89823 89559
88545 89823
89143 88545
89870 89143
89405 89870
87780 89405
88831 87780
87330 88831
88770 87330
89085 88770
88362 89085
89869 88362
89642 89869
88250 89642
89505 88250
89948 89505
89449 ...

result:

ok both subtasks are correct!

Test #14:

score: 5
Accepted
time: 16ms
memory: 20900kb

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
72763 80401
80201 72763
78793 80201
79035 78793
79437 79035
79747 79437
79195 79747
79999 79195
80202 79999
79798 80202
78682 79798
79597 78682
79799 79597
78391 79799
79196 78391
79800 79196
78794 79800
79801 78794
80342 79801
77989 80342
79599 77989
78795 79599
77990 78795
79198 77990
80203 ...

result:

ok both subtasks are correct!

Test #15:

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

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
17378 160001
158978 17378
90767 158978
144767 90767
160001 144767
7535 160001
153935 7535
152042 153935
48726 152042
98416 48726
9216 98416
3216 9216
48416 3216
36411 48416
126469 36411
42015 126469
40418 42015
87618 40418
38828 87618
30986 38828
24133 30986
4...

result:

ok both subtasks are correct!

Test #16:

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

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
58479 60201
59728 58479
59959 59728
59598 59959
59906 59598
59782 59906
59369 59782
59041 59369
59894 59041
59680 59894
59471 59680
59948 59471
59131 59948
59950 59131
58492 59950
59864 58492
59616 59864
58909 59616
59061 58909
60195 59061
59423 60195
59870 59423
59418 59870
59601 59418
60015 ...

result:

ok both subtasks are correct!

Test #17:

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

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
403 80401
80200 403
79597 80200
78549 79597
79598 78549
79999 79598
80201 79999
78391 80201
79599 78391
80202 79599
79798 80202
78392 79798
78793 78392
80203 78793
79396 80203
79799 79396
79397 79799
78393 79397
80205 78393
77989 80205
80206 77989
77587 80206
80207 77587
77185 80207
80208 7718...

result:

ok both subtasks are correct!

Test #18:

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

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
118674 120001
119003 118674
117902 119003
119911 117902
115912 119911
119998 115912
119587 119998
119278 119587
119856 119278
118310 119856
119958 118310
118381 119958
119444 118381
118959 119444
119732 118959
119261 119732
119747 119261
118842 119747
119546 118842
118883 119546
119431 118883
...

result:

ok both subtasks are correct!

Test #19:

score: 5
Accepted
time: 29ms
memory: 28400kb

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
121367 132868
132535 121367
131537 132535
132202 131537
132536 132202
131848 132536
132538 131848
131203 132538
132203 131203
131539 132203
132539 131539
130870 132539
132205 130870
130204 132205
132206 130204
131204 132206
132540 131204
129871 132540
132208 129871
130871 132208
132541 130871...

result:

ok both subtasks are correct!

Test #20:

score: 5
Accepted
time: 20ms
memory: 20540kb

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
157524 160001
159631 157524
158498 159631
158863 158498
158232 158863
159214 158232
158914 159214
159605 158914
159382 159605
159817 159382
158332 159817
159591 158332
157927 159591
158586 157927
159807 158586
158470 159807
156203 158470
158420 156203
159997 158420
158467 159997
156333 158467
...

result:

ok both subtasks are correct!