QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#168416#5280. Depot RearrangementCyanmond46 62ms24632kbC++172.4kb2023-09-08 14:29:452023-09-08 14:29:45

Judging History

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

  • [2023-09-08 14:29:45]
  • 评测
  • 测评结果:46
  • 用时:62ms
  • 内存:24632kb
  • [2023-09-08 14:29:45]
  • 提交

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({p, e});
            p = e;
        }
        ways.push_back({p, N * M});
    }

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

result:

ok both subtasks are correct!

Test #3:

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

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #4:

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #5:

score: 2
Acceptable Answer
time: 4ms
memory: 3964kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #6:

score: 2
Acceptable Answer
time: 2ms
memory: 3952kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #7:

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

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #8:

score: 2
Acceptable Answer
time: 6ms
memory: 5620kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #9:

score: 2
Acceptable Answer
time: 8ms
memory: 6560kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #10:

score: 2
Acceptable Answer
time: 0ms
memory: 3816kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #11:

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

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #12:

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

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #13:

score: 2
Acceptable Answer
time: 20ms
memory: 10736kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #14:

score: 2
Acceptable Answer
time: 26ms
memory: 15420kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #15:

score: 2
Acceptable Answer
time: 17ms
memory: 8404kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #16:

score: 2
Acceptable Answer
time: 13ms
memory: 8636kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #17:

score: 2
Acceptable Answer
time: 31ms
memory: 17676kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #18:

score: 2
Acceptable Answer
time: 19ms
memory: 13804kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #19:

score: 2
Acceptable Answer
time: 62ms
memory: 24632kb

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

result:

points 0.40 first subtask is correct but plan is wrong.

Test #20:

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

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

result:

points 0.40 first subtask is correct but plan is wrong.