QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#168418#5280. Depot RearrangementCyanmond46 52ms24752kbC++172.4kb2023-09-08 14:31:162023-09-08 14:31:17

Judging History

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

  • [2023-09-08 14:31:17]
  • 评测
  • 测评结果:46
  • 用时:52ms
  • 内存:24752kb
  • [2023-09-08 14:31:16]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define per(i, l, r) for (int i = (r - 1); i >= (l); --i)
#define ALL(x) (x).begin(), (x).end()

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> A(N * M);
    for (auto &e : A) {
        cin >> e;
        --e;
    }

    vector<vector<int>> fCount(N, vector<int>(M));
    vector<bool> isT(N * M);
    rep (i, 0, N) {
        rep (j, i * M, (i + 1) * M) {
            if (++fCount[i][A[j]] >= 2) {
                isT[j] = true;
            }
        }
    }

    vector<vector<int>> graph(N * M + N + M);
    rep (i, 0, N * M) {
        if (isT[i]) {
            graph[i].push_back(N * M + N + A[i]);
        }
    }
    rep (i, 0, N) {
        int p = 0;
        rep (j, 0, M) {
            if (fCount[i][j] == 0) {
                graph[N * M + N + j].push_back(N * M + i);
                while (not isT[i * M + p]) {
                    ++p;
                }
                graph[N * M + i].push_back(i * M + p);
                ++p;
            }
        }
    }

    /*
    rep (i, 0, N * M + N + M) {
        for (const auto t : graph[i]) {
            cout << i << ' ' << t << endl;
        }
    }
    cout << endl;
    */

    vector<bool> isSeen(2 * N * M + M);
    auto dfs = [&](auto &&self, const int v, vector<int> &trail) -> void {
        isSeen[v] = true;
        while (not graph[v].empty()) {
            const int t = graph[v].back();
            graph[v].pop_back();
            self(self, t, trail);
        }
        trail.push_back(v);
    };

    int ans = 0;
    vector<pair<int, int>> ways;
    rep (f, 0, N * M) {
        if (isSeen[f]) continue;
        if (graph[f].empty()) continue;
        vector<int> trail;
        dfs(dfs, f, trail);
        reverse(ALL(trail));
        vector<int> path;
        for (const auto e : trail) {
            if (e < N * M) {
                path.push_back(e);
            }
        }
        // assert(path[0] == path[path.size() - 1]);
        ans += path.size();

        path.pop_back();
        int p = N * M;
        for (const auto e : path) {
            ways.push_back({e, p});
            p = e;
        }
        ways.push_back({N * M, p});
    }

    cout << ans << '\n';
    for (const auto &[a, b] : ways) cout << a + 1 << ' ' << b + 1 << '\n';
}

详细

Test #1:

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

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

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

result:

ok both subtasks are correct!

Test #3:

score: 2
Acceptable Answer
time: 1ms
memory: 3624kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #4:

score: 2
Acceptable Answer
time: 1ms
memory: 3644kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #5:

score: 2
Acceptable Answer
time: 1ms
memory: 3948kb

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
2648 273
16173 2648
16885 16173
19298 16885
19398 19298
18899 19398
9994 18899
17774 9994
7068 17774
18968 7068
4495 18968
4895 4495
4482 4895
19682 4482
19198 19682
17199 19198
6599 17199
18491 6599
18091 18491
18488 18091
10488 18488
18466 10488
18966 18466
16367 18966
17266 16367
15...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #6:

score: 2
Acceptable Answer
time: 1ms
memory: 3924kb

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
3980 4
4000 3980
3920 4000
4020 3920
3999 4020
3918 3999
3960 3918
3917 3960
3940 3917
3979 3940
3800 3979
4016 3800
3936 4016
3760 3936
4015 3760
3978 4015
3658 3978
3957 3658
4013 3957
3994 4013
3935 3994
3992 3935
3915 3992
3860 3915
3914 3860
3932 3914
3899 3932
3858 3899
4011 3858
3...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #7:

score: 2
Acceptable Answer
time: 3ms
memory: 6004kb

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
72733 1262
75843 72733
77677 75843
87877 77677
51995 87877
6695 51995
64122 6695
40757 64122
38351 40757
41351 38351
38357 41351
40722 38357
63995 40722
46712 63995
84512 46712
46595 84512
51930 46595
68242 51930
83209 68242
81409 83209
83192 81409
47363 83192
7031 47363
78731 7031
71...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #8:

score: 2
Acceptable Answer
time: 7ms
memory: 5616kb

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
12040 2
11720 12040
12039 11720
11680 12039
12000 11680
11640 12000
11960 11640
11600 11960
11920 11600
11560 11920
11880 11560
11520 11880
11840 11520
11480 11840
11800 11480
11440 11800
11400 11440
12038 11400
11399 12038
11999 11399
11360 11999
11959 11360
11320 11959
11919 11320
11...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #9:

score: 2
Acceptable Answer
time: 7ms
memory: 6664kb

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
39900 7
40000 39900
39799 40000
39699 39799
39998 39699
39600 39998
39997 39600
39797 39997
39596 39797
39899 39596
39996 39899
39898 39996
39995 39898
39896 39995
39994 39896
39698 39994
39795 39698
39499 39795
39993 39499
39794 39993
39697 39794
39793 39697
39400 39793
39695 39400
39...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #10:

score: 2
Acceptable Answer
time: 3ms
memory: 3820kb

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
4639 17
5439 4639
6400 5439
6240 6400
4320 6240
6080 4320
5600 6080
5436 5600
6399 5436
5119 6399
4638 5119
6238 4638
5598 6238
5435 5598
4159 5435
5920 4159
5117 5920
4800 5117
6393 4800
5431 6393
6077 5431
4637 6077
3518 4637
5280 3518
4960 5280
5754 4960
6237 5754
6073 6237
5113 6073...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #11:

score: 2
Acceptable Answer
time: 11ms
memory: 6628kb

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
40000 9
39499 40000
39899 39499
39700 39899
39998 39700
39897 39998
39796 39897
39896 39796
39997 39896
39697 39997
39894 39697
39198 39894
39795 39198
39996 39795
39696 39996
39598 39696
39793 39598
39892 39793
39596 39892
39498 39596
39693 39498
39890 39693
39300 39890
39398 39300
39...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #12:

score: 2
Acceptable Answer
time: 3ms
memory: 4508kb

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
6020 3
5700 6020
6018 5700
5680 6018
5980 5680
5660 5980
5960 5660
5640 5960
5939 5640
6017 5939
5619 6017
5920 5619
5599 5920
5900 5599
5580 5900
5880 5580
5560 5880
5820 5560
5540 5820
5800 5540
5480 5800
5760 5480
5440 5760
5398 5440
6016 5398
5420 6016
5380 5420
6000 5380
5397 6000
5...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #13:

score: 2
Acceptable Answer
time: 22ms
memory: 10812kb

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
89699 15
88500 89699
90000 88500
89698 90000
88800 89698
89999 88800
89697 89999
89100 89697
87900 89100
89696 87900
89998 89696
89694 89998
89996 89694
89400 89996
88799 89400
89995 88799
88498 89995
89099 88498
87600 89099
89097 87600
88798 89097
87599 88798
89993 87599
89692 89993
...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #14:

score: 2
Acceptable Answer
time: 22ms
memory: 15444kb

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
80400 2
80000 80400
79600 80000
79199 79600
79999 79199
79198 79999
78800 79198
80398 78800
79196 80398
78400 79196
79998 78400
80397 79998
79997 80397
79599 79997
78799 79599
79996 78799
79195 79996
78000 79195
80393 78000
78798 80393
79995 78798
78797 79995
79597 78797
77999 79597
78...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #15:

score: 2
Acceptable Answer
time: 23ms
memory: 8516kb

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
11722 2922
3172 11722
115020 3172
45736 115020
13003 45736
62203 13003
52743 62203
112743 52743
148343 112743
98343 148343
95543 98343
49143 95543
12909 49143
132351 12909
94351 132351
119963 94351
152588 119963
138000 152588
139600 138000
137788 1...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #16:

score: 2
Acceptable Answer
time: 14ms
memory: 8692kb

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
60000 13
59800 60000
59996 59800
59600 59996
59994 59600
59198 59994
59400 59198
59799 59400
58599 59799
59993 58599
59596 59993
59797 59596
59992 59797
59196 59992
59991 59196
58598 59991
59990 58598
59595 59990
60200 59595
59194 60200
59000 59194
59796 59000
59988 59796
60199 59988
...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #17:

score: 2
Acceptable Answer
time: 36ms
memory: 17656kb

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
80400 2
80000 80400
79600 80000
80399 79600
79599 80399
78800 79599
79598 78800
78400 79598
80397 78400
78799 80397
78399 78799
79998 78399
80396 79998
78398 80396
79596 78398
80395 79596
78000 80395
80394 78000
77600 80394
80393 77600
77200 80393
80392 77200
76800 80392
80391 76800
76...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #18:

score: 2
Acceptable Answer
time: 24ms
memory: 13628kb

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
120000 26
116097 120000
119999 116097
119398 119999
119700 119398
119997 119700
118200 119997
119100 118200
119699 119100
118499 119699
119993 118499
118497 119993
119989 118497
119096 119989
118800 119096
119093 118800
119987 119093
119393 119987
119985 119393
119694 119985
119092 1...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #19:

score: 2
Acceptable Answer
time: 52ms
memory: 24752kb

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
2 132868
132867 2
132468 132867
131670 132468
132865 131670
132069 132865
132864 132069
131669 132864
132467 131669
131271 132467
132862 131271
131270 132862
132466 131270
130872 132466
132861 130872
130871 132861
132464 130871
130473 132464
132463 130473
130073 132463
132858 130073
130072 13...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #20:

score: 2
Acceptable Answer
time: 39ms
memory: 16780kb

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
26 160001
160000 26
159197 160000
158800 159197
159999 158800
159594 159999
158399 159594
159195 158399
159592 159195
159998 159592
158798 159998
157999 158798
159591 157999
158398 159591
159997 158398
158795 159997
159996 158795
158397 159996
156400 158397
158794 156400
156399 158794
158793 1...

result:

points 0.40 first subtask is correct but plan is wrong.