QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116089#5280. Depot Rearrangementhhoppitree#100 ✓31ms21968kbC++141.2kb2023-06-28 09:14:022024-05-31 14:20:28

Judging History

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

  • [2024-05-31 14:20:28]
  • 评测
  • 测评结果:100
  • 用时:31ms
  • 内存:21968kb
  • [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:14:02]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1005;

int ed, vis[N];
vector< pair<int, int> > G[N];
vector<int> z;

void dfs(int x)
{
    int v, w;
    while (G[x].size()) tie(v, w) = G[x].back(), G[x].pop_back(), dfs(v), w && (z.push_back(w), 0);
}

vector< pair<int, int> > tp;

void add()
{
    tp.push_back(make_pair(z[0], ed));
    for (int i = 1; i < z.size(); ++i) tp.push_back(make_pair(z[i], z[i - 1]));
    tp.push_back(make_pair(ed, z.back()));
    return;
}

signed main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    ed = n * m + 1;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1, x; j <= m; ++j) {
            scanf("%d", &x);
            if (vis[x]++) G[x].push_back(make_pair(m + i, (i - 1) * m + j));
        }
        for (int j = 1; j <= m; vis[j++] = 0) if (!vis[j])
            G[m + i].push_back(make_pair(j, 0));
    }
    for (int i = 1; i <= m + n; ++i) vis[i] = 0;
    for (int i = 1; i <= m; ++i) {
        if (G[i].size() && !vis[i]) {
            z.clear();
            dfs(i);
			reverse(z.begin(), z.end());
            add();
        }
    }
    printf("%d\n", tp.size());
    for (auto [x, y] : tp) printf("%d %d\n", x, y);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

result:

ok both subtasks are correct!

Test #3:

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

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

result:

ok both subtasks are correct!

Test #4:

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

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

result:

ok both subtasks are correct!

Test #5:

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

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
19529 20001
18829 19529
15999 18829
19184 15999
19298 19184
16885 19298
16173 16885
10355 16173
18781 10355
10381 18781
17855 10381
18091 17855
18491 18091
14288 18491
3013 14288
19047 3013
6955 19047
16555 6955
6247 16555
19747 6247
4647 19747
12213 4647
1488 12213
10488 1488
18488 10488
14671 ...

result:

ok both subtasks are correct!

Test #6:

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

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
3935 4021
3872 3935
4015 3872
3994 4015
4020 3994
3992 4020
3871 3992
3977 3871
3857 3977
3899 3857
3966 3899
3914 3966
3957 3914
3798 3957
3832 3798
3794 3832
3780 3794
3652 3780
4013 3652
3912 4013
3760 3912
3640 3760
3776 3640
3874 3776
3980 3874
3940 3980
3913 3940
3894 3913
3932 3894
3758 ...

result:

ok both subtasks are correct!

Test #7:

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

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
73649 90001
85356 73649
31656 85356
26374 31656
16773 26374
27271 16773
61771 27271
58544 61771
85710 58544
20910 85710
54956 20910
13556 54956
42644 13556
57662 42644
82270 57662
57670 82270
1262 57670
72662 1262
11820 72662
72720 11820
75733 72720
37143 75733
87877 37143
84877 87877
35684 8487...

result:

ok both subtasks are correct!

Test #8:

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

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
301 12041
12040 301
11739 12040
11438 11739
12039 11438
11437 12039
11137 11437
12038 11137
11136 12038
11738 11136
11135 11738
10836 11135
12037 10836
10835 12037
11737 10835
10834 11737
11436 10834
10833 11436
10535 10833
12036 10535
10534 12036
11736 10534
10234 11736
12035 10234
10233 1203...

result:

ok both subtasks are correct!

Test #9:

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

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
39765 40001
39674 39765
39983 39674
39699 39983
39775 39699
39835 39775
39648 39835
39278 39648
39631 39278
39249 39631
39867 39249
39986 39867
39659 39986
39574 39659
39626 39574
39555 39626
39958 39555
39588 39958
39173 39588
39476 39173
38765 39476
38999 38765
37985 38999
39952 37985
39569 ...

result:

ok both subtasks are correct!

Test #10:

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

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
6047 6401
5749 6047
6072 5749
5907 6072
4636 5907
5465 4636
6400 5465
5439 6400
6077 5439
6317 6077
6225 6317
6080 6225
5101 6080
5920 5101
4319 5920
5280 4319
6238 5280
5754 6238
6070 5754
6378 6070
5744 6378
5271 5744
3836 5271
5435 3836
3516 5435
6393 3516
5894 6393
6391 5894
6204 6391
5270 ...

result:

ok both subtasks are correct!

Test #11:

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

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
39755 40001
39566 39755
39467 39566
39795 39467
39873 39795
39390 39873
39266 39390
38947 39266
38763 38947
39936 38763
38484 39936
38920 38484
38570 38920
38296 38570
38692 38296
38798 38692
39462 38798
39368 39462
39995 39368
38469 39995
38588 38469
39899 38588
39956 39899
39334 39956
39994 ...

result:

ok both subtasks are correct!

Test #12:

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

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
5932 6021
5996 5932
5617 5996
6020 5617
5719 6020
5417 5719
6018 5417
5416 6018
4815 5416
6017 4815
5200 6017
6016 5200
4987 6016
6015 4987
5572 6015
6014 5572
5976 6014
5718 5976
5300 5718
6013 5300
5668 6013
6012 5668
5939 6012
5415 5939
4883 5415
5717 4883
5199 5717
5117 5199
6011 5117
4912 ...

result:

ok both subtasks are correct!

Test #13:

score: 5
Accepted
time: 12ms
memory: 8804kb

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
89586 90001
87550 89586
89694 87550
89998 89694
87217 89998
89986 87217
89370 89986
89593 89370
89254 89593
89080 89254
89365 89080
88089 89365
88455 88089
87112 88455
89364 87112
87799 89364
86762 87799
88478 86762
87894 88478
89398 87894
89088 89398
86694 89088
83183 86694
88198 83183
87595 ...

result:

ok both subtasks are correct!

Test #14:

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

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
58310 80401
80400 58310
79998 80400
80398 79998
79596 80398
80199 79596
79194 80199
80393 79194
78992 80393
80197 78992
78792 80197
80392 78792
78390 80392
80391 78390
78187 80391
80196 78187
77985 80196
80390 77985
77787 80390
80195 77787
79145 80195
79997 79145
79595 79997
79996 79595
80356 ...

result:

ok both subtasks are correct!

Test #15:

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

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
158790 160001
25190 158790
88665 25190
62569 88665
115932 62569
83888 115932
83273 83888
90073 83273
8955 90073
148555 8955
134930 148555
126916 134930
143716 126916
110130 143716
119755 110130
53963 119755
117163 53963
139563 117163
114769 139563
139569 114769
138000 139569
139600 138000
149561...

result:

ok both subtasks are correct!

Test #16:

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

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
58389 60201
60185 58389
59929 60185
60114 59929
59940 60114
59790 59940
60143 59790
58891 60143
60088 58891
57973 60088
59889 57973
60107 59889
58726 60107
60125 58726
58161 60125
60000 58161
57783 60000
60150 57783
59984 60150
58349 59984
59899 58349
59357 59899
60076 59357
59988 60076
57597 ...

result:

ok both subtasks are correct!

Test #17:

score: 5
Accepted
time: 14ms
memory: 15904kb

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
201 80401
80400 201
79998 80400
80399 79998
79797 80399
79596 79797
80397 79596
79595 80397
80199 79595
79394 80199
79997 79394
79393 79997
79193 79393
80198 79193
79192 80198
79996 79192
79191 79996
79594 79191
78993 79594
78792 78993
80396 78792
78791 80396
80196 78791
78591 80196
79995 7859...

result:

ok both subtasks are correct!

Test #18:

score: 5
Accepted
time: 12ms
memory: 10756kb

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
119581 120001
118794 119581
119982 118794
117811 119982
118795 117811
118940 118795
119654 118940
119883 119654
119692 119883
119296 119692
117594 119296
118192 117594
119525 118192
118488 119525
119699 118488
118739 119699
119960 118739
119069 119960
119956 119069
118657 119956
119091 118657
...

result:

ok both subtasks are correct!

Test #19:

score: 5
Accepted
time: 31ms
memory: 21968kb

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
23021 132868
132867 23021
132201 132867
132865 132201
131867 132865
132864 131867
131535 132864
132862 131535
131202 132862
132861 131202
130869 132861
132858 130869
130203 132858
132534 130203
129870 132534
132857 129870
129537 132857
132856 129537
129204 132856
132854 129204
129882 132854
1...

result:

ok both subtasks are correct!

Test #20:

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

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
158345 160001
160000 158345
157793 160000
153428 157793
159158 153428
159997 159158
159039 159997
159991 159039
157599 159991
159550 157599
159113 159550
155589 159113
159024 155589
154932 159024
156577 154932
149599 156577
159195 149599
157984 159195
158798 157984
154800 158798
157198 154800
...

result:

ok both subtasks are correct!