QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#141537#5280. Depot Rearrangementtatyam#90 36ms17372kbC++172.8kb2023-08-17 16:09:172024-07-04 01:46:44

Judging History

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

  • [2024-07-04 01:46:44]
  • 评测
  • 测评结果:90
  • 用时:36ms
  • 内存:17372kb
  • [2023-08-17 16:09:17]
  • 提交

answer

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

/*
 各グループで, 各種類 1 個ずつ位置を確定, 確定しなかった位置 → その種類の倉庫 に辺を張り, 足りない種類の倉庫 → グループの倉庫 → 確定しなかった位置 に辺を張り, できるだけ少ない個数のサイクルに分解する
 ところでこれは有向オイラーグラフなので (強) 連結成分数 個のサイクルに分解可能
 有向オイラーグラフからオイラーパスを取り出す方法: 辺を削除しながら DFS する. 行き止まりになったら 1 段階戻り, オイラーパスの最後の辺を確定. 後ろから順に決まっていく.
 (これは無向のときでした) 計算量を壊さないように current-edge data structure と, erased[辺番号] := その辺が削除されたか のフラグ, 辺に辺番号 が必要
 有向なら pop_back で良いです
 */
int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    int N, M;
    cin >> N >> M;
    const int NM = N * M;
    vector<int> A(NM);
    for(int& a : A) {
        cin >> a;
        a--;
    }
    
    const auto group = [&](int i) { return NM + i; };
    const auto number = [&](int x) { return NM + N + x; };
    vector g(NM + N + M, vector<int>{});
    auto add_edge = [&](int i, int j) {
        g[i].push_back(j);
    };
    
    for(int i = 0; i < N; i++) {
        vector<bool> num(M);
        for(int j = 0; j < M; j++) {
            const int at = i * M + j, x = A[at];
            if(num[x]) {
                add_edge(at, number(x));
                add_edge(group(i), at);
            }
            else num[x] = 1;
        }
        for(int x = 0; x < M; x++) if(!num[x]) add_edge(number(x), group(i));
    }
    
    vector<pair<int, int>> ans;
    A.push_back(-1);
    auto query = [&](int i, int j) {
        assert(A[j] == -1);
        swap(A[i], A[j]);
        ans.emplace_back(i, j);
    };
    
    for(int at = 0; at < NM; at++) if(g[at].size()) {
        vector<int> cycle = {at};
        auto dfs = [&](int at, auto dfs) -> void {
            while(g[at].size()) {
                const int to = g[at].back();
                g[at].pop_back();
                dfs(to, dfs);
                if(at < NM) cycle.push_back(at);
            }
        };
        dfs(at, dfs);
        cycle.pop_back();
        query(at, NM);
        for(int i = 1; i < cycle.size(); i++) query(cycle[i], cycle[i - 1]);
        query(NM, cycle.back());
    }
    
    cout << ans.size() << '\n';
    for(auto [i, j] : ans) cout << i + 1 << ' ' << j + 1 << '\n';
    
    for(int i = 0; i < N; i++) {
        vector<bool> num(M);
        for(int j = 0; j < M; j++) {
            const int at = i * M + j, x = A[at];
            assert(!num[x]);
            num[x] = 1;
        }
    }
}

詳細信息

Test #1:

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

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: 1ms
memory: 3844kb

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

result:

ok both subtasks are correct!

Test #3:

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

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

result:

ok both subtasks are correct!

Test #4:

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

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

result:

ok both subtasks are correct!

Test #5:

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

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
13073 273
12213 13073
10354 12213
7854 10354
459 7854
3259 459
1459 3259
5159 1459
1862 5159
3158 1862
1858 3158
3192 1858
1892 3192
7562 1892
8849 7562
475 8849
1541 475
17469 1541
1569 17469
4941 1569
16555 4941
6955 16555
17855 6955
4955 17855
15140 4955
11540 15140
10355 11540
1155...

result:

ok both subtasks are correct!

Test #6:

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

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
4 4021
47 4
28 47
64 28
29 64
164 29
11 164
84 11
33 84
168 33
88 168
51 88
344 51
34 344
231 34
36 231
92 36
53 92
206 53
104 206
70 104
12 70
262 12
74 262
106 74
76 106
126 76
245 126
186 245
210 186
37 210
303 37
290 303
252 290
93 252
144 93
384 144
369 384
306 369
446 306
326 446
107 326
...

result:

ok both subtasks are correct!

Test #7:

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

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
68130 37143
22642 68130
36376 22642
38476 36376
39588 38476
77388 39588
69294 77388
1916 69294
55164 1916
13556 55164
85710 13556
20910 85710
42644 20910
44971 42644
53371 44971
26373 53371
19174 26373
85356 19174
31656 85356
...

result:

ok both subtasks are correct!

Test #8:

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

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
2 12041
322 2
42 322
362 42
82 362
402 82
122 402
442 122
162 442
482 162
202 482
522 202
242 522
562 242
3 562
642 3
602 642
43 602
643 43
83 643
682 83
123 682
722 123
163 722
762 163
203 762
802 203
243 802
842 243
282 842
683 282
323 683
723 323
363 723
763 363
403 763
803 403
443 803
843 ...

result:

ok both subtasks are correct!

Test #9:

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

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
7 40001
206 7
22 206
512 22
113 512
319 113
24 319
210 24
118 210
31 118
1005 31
215 1005
713 215
32 713
125 32
418 125
613 418
218 613
128 218
33 128
513 33
329 513
129 329
34 129
917 34
130 917
219 130
40 219
520 40
230 520
821 230
41 821
133 41
715 133
239 715
136 239
330 136
720 330
627 72...

result:

ok both subtasks are correct!

Test #10:

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

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
17 6401
338 17
1936 338
977 1936
806 977
182 806
504 182
1174 504
28 1174
831 28
666 831
189 666
40 189
1470 40
45 1470
353 45
192 353
839 192
705 839
52 705
1178 52
714 1178
56 714
2417 56
195 2417
1631 195
58 1631
366 58
1803 366
205 1803
1328 205
226 1328
841 226
1189 841
71 1189
857 71
229 ...

result:

ok both subtasks are correct!

Test #11:

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

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
9 40001
119 9
12 119
311 12
16 311
207 16
18 207
132 18
20 132
507 20
317 507
24 317
134 24
25 134
820 25
138 820
329 138
30 329
603 30
419 603
36 419
331 36
709 331
140 709
339 140
1314 339
727 1314
430 727
37 430
905 37
340 905
45 340
141 45
46 141
1421 46
341 1421
208 341
49 208
611 49
51 6...

result:

ok both subtasks are correct!

Test #12:

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

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
3 6021
322 3
23 322
343 23
42 343
362 42
63 362
383 63
82 383
402 82
123 402
423 123
143 423
445 143
162 445
482 162
203 482
502 203
4 502
522 4
242 522
544 242
262 544
602 262
5 602
622 5
25 622
642 25
44 642
665 44
64 665
723 64
84 723
742 84
102 742
763 102
124 763
784 124
144 784
802 144
6 ...

result:

ok both subtasks are correct!

Test #13:

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

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
15 90001
309 15
1213 309
312 1213
40 312
916 40
49 916
945 49
1813 945
962 1813
53 962
332 53
55 332
631 55
335 631
57 335
966 57
68 966
2726 68
637 2726
350 637
644 350
2730 644
354 2730
648 354
360 648
1521 360
1816 1521
361 1816
69 361
363 69
2148 363
1217 2148
82 1217
1238 82
83 1238
1243 ...

result:

ok both subtasks are correct!

Test #14:

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

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
2 80401
404 2
3 404
802 3
4 802
1602 4
1203 1602
5 1203
2002 5
1204 2002
803 1204
405 803
1603 405
804 1603
1604 804
7 1604
2402 7
1205 2402
406 1205
2003 406
8 2003
809 8
2004 809
1605 2004
408 1605
2404 408
9 2404
3202 9
1206 3202
2005 1206
409 2005
810 409
2802 810
10 2802
1208 10
811 1208
...

result:

ok both subtasks are correct!

Test #15:

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

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
2922 160001
147572 2922
133420 147572
87336 133420
132109 87336
90155 132109
48288 90155
62732 48288
19769 62732
71849 19769
132724 71849
49916 132724
119484 49916
6527 119484
129727 6527
84927 129727
119327 84927
49884 119327
45116 49884
36387 45116
31187 363...

result:

ok both subtasks are correct!

Test #16:

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

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
13 60201
1228 13
220 1228
621 220
16 621
221 16
1234 221
2808 1234
25 2808
231 25
3208 231
232 3208
29 232
627 29
405 627
244 405
4812 244
32 4812
630 32
416 630
824 416
417 824
633 417
1246 633
637 1246
5235 637
246 5235
1011 246
1247 1011
419 1247
250 419
830 250
438 830
252 438
6224 252
253...

result:

ok both subtasks are correct!

Test #17:

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

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
2 80401
802 2
3 802
1202 3
803 1202
402 803
1203 402
4 1203
1604 4
804 1604
404 804
1605 404
5 1605
2002 5
6 2002
2402 6
7 2402
2802 7
8 2802
3202 8
9 3202
3602 9
10 3602
4002 10
11 4002
4402 11
12 4402
4802 12
13 4802
5202 13
15 5202
5602 15
16 5602
6003 16
17 6003
6402 17
18 6402
6802 18
19 ...

result:

ok both subtasks are correct!

Test #18:

score: 5
Accepted
time: 19ms
memory: 13568kb

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
26 120001
323 26
28 323
3616 28
43 3616
325 43
48 325
615 48
1552 615
1216 1552
53 1216
337 53
1554 337
617 1554
1241 617
924 1241
65 924
5138 65
645 5138
1555 645
1248 1555
351 1248
1253 351
354 1253
1259 354
372 1259
66 372
1556 66
647 1556
75 647
649 75
1823 649
1561 1823
76 1561
943 76
653...

result:

ok both subtasks are correct!

Test #19:

score: 0
Runtime Error

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:


result:


Test #20:

score: 0
Judgement Failed

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:


result: