QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#168413#5280. Depot RearrangementCyanmond5 148ms24572kbC++172.4kb2023-09-08 14:27:542023-09-08 14:27:54

Judging History

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

  • [2023-09-08 14:27:54]
  • 评测
  • 测评结果:5
  • 用时:148ms
  • 内存:24572kb
  • [2023-09-08 14:27:54]
  • 提交

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();

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

    cout << ans << endl;
    for (const auto &[a, b] : ways) cout << a << ' ' << b << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Wrong Answer
time: 1ms
memory: 3616kb

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

result:

wrong output format Extra information in the output file

Test #3:

score: 0
Wrong Answer
time: 1ms
memory: 3820kb

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
100 2
2 89
89 29
29 99
99 49
49 78
78 57
57 96
96 88
88 94
94 28
28 66
66 55
55 38
38 48
48 86
86 27
27 75
75 65
65 54
54 74
74 43
43 37
37 19
19 35
35 5
5 26
26 42
42 15
15 25
25 17
17 2
2 100

result:

wrong output format Extra information in the output file

Test #4:

score: 0
Wrong Answer
time: 1ms
memory: 3656kb

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
1000 108
108 948
948 818
818 712
712 733
733 849
849 669
669 829
829 209
209 174
174 774
774 358
358 813
813 183
183 452
452 108
108 1000
1000 706
706 746
746 706
706 1000

result:

wrong output format Extra information in the output file

Test #5:

score: 0
Wrong Answer
time: 4ms
memory: 3956kb

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
20000 272
272 2647
2647 16172
16172 16884
16884 19297
19297 19397
19397 18898
18898 9993
9993 17773
17773 7067
7067 18967
18967 4494
4494 4894
4894 4481
4481 19681
19681 19197
19197 17198
17198 6598
6598 18490
18490 18090
18090 18487
18487 10487
10487 18465
18465 18965
18965 16366
16366 17265
17...

result:

wrong output format Extra information in the output file

Test #6:

score: 0
Wrong Answer
time: 2ms
memory: 3976kb

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
4020 3
3 3979
3979 3999
3999 3919
3919 4019
4019 3998
3998 3917
3917 3959
3959 3916
3916 3939
3939 3978
3978 3799
3799 4015
4015 3935
3935 3759
3759 4014
4014 3977
3977 3657
3657 3956
3956 4012
4012 3993
3993 3934
3934 3991
3991 3914
3914 3859
3859 3913
3913 3931
3931 3898
3898 3857
3857 4010
4...

result:

wrong output format Extra information in the output file

Test #7:

score: 0
Wrong Answer
time: 15ms
memory: 6012kb

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
90000 1261
1261 72732
72732 75842
75842 77676
77676 87876
87876 51994
51994 6694
6694 64121
64121 40756
40756 38350
38350 41350
41350 38356
38356 40721
40721 63994
63994 46711
46711 84511
84511 46594
46594 51929
51929 68241
68241 83208
83208 81408
81408 83191
83191 47362
47362 7030
7030 78730
78...

result:

wrong output format Extra information in the output file

Test #8:

score: 0
Wrong Answer
time: 24ms
memory: 5588kb

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
12040 1
1 12039
12039 11719
11719 12038
12038 11679
11679 11999
11999 11639
11639 11959
11959 11599
11599 11919
11919 11559
11559 11879
11879 11519
11519 11839
11839 11479
11479 11799
11799 11439
11439 11399
11399 12037
12037 11398
11398 11998
11998 11359
11359 11958
11958 11319
11319 11918
11...

result:

wrong output format Extra information in the output file

Test #9:

score: 0
Wrong Answer
time: 22ms
memory: 6600kb

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
40000 6
6 39899
39899 39999
39999 39798
39798 39698
39698 39997
39997 39599
39599 39996
39996 39796
39796 39595
39595 39898
39898 39995
39995 39897
39897 39994
39994 39895
39895 39993
39993 39697
39697 39794
39794 39498
39498 39992
39992 39793
39793 39696
39696 39792
39792 39399
39399 39694
39...

result:

wrong output format Extra information in the output file

Test #10:

score: 0
Wrong Answer
time: 3ms
memory: 3908kb

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
6400 16
16 4638
4638 5438
5438 6399
6399 6239
6239 4319
4319 6079
6079 5599
5599 5435
5435 6398
6398 5118
5118 4637
4637 6237
6237 5597
5597 5434
5434 4158
4158 5919
5919 5116
5116 4799
4799 6392
6392 5430
5430 6076
6076 4636
4636 3517
3517 5279
5279 4959
4959 5753
5753 6236
6236 6072
6072 5112...

result:

wrong output format Extra information in the output file

Test #11:

score: 0
Wrong Answer
time: 22ms
memory: 6704kb

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
40000 8
8 39999
39999 39498
39498 39898
39898 39699
39699 39997
39997 39896
39896 39795
39795 39895
39895 39996
39996 39696
39696 39893
39893 39197
39197 39794
39794 39995
39995 39695
39695 39597
39597 39792
39792 39891
39891 39595
39595 39497
39497 39692
39692 39889
39889 39299
39299 39397
39...

result:

wrong output format Extra information in the output file

Test #12:

score: 0
Wrong Answer
time: 8ms
memory: 4272kb

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
6020 2
2 6019
6019 5699
5699 6017
6017 5679
5679 5979
5979 5659
5659 5959
5959 5639
5639 5938
5938 6016
6016 5618
5618 5919
5919 5598
5598 5899
5899 5579
5579 5879
5879 5559
5559 5819
5819 5539
5539 5799
5799 5479
5479 5759
5759 5439
5439 5397
5397 6015
6015 5419
5419 5379
5379 5999
5999 5396
5...

result:

wrong output format Extra information in the output file

Test #13:

score: 0
Wrong Answer
time: 51ms
memory: 10764kb

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
90000 14
14 89698
89698 88499
88499 89999
89999 89697
89697 88799
88799 89998
89998 89696
89696 89099
89099 87899
87899 89695
89695 89997
89997 89693
89693 89995
89995 89399
89399 88798
88798 89994
89994 88497
88497 89098
89098 87599
87599 89096
89096 88797
88797 87598
87598 89992
89992 89691
...

result:

wrong output format Extra information in the output file

Test #14:

score: 0
Wrong Answer
time: 87ms
memory: 15376kb

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
80400 1
1 80399
80399 79999
79999 79599
79599 79198
79198 79998
79998 79197
79197 78799
78799 80397
80397 79195
79195 78399
78399 79997
79997 80396
80396 79996
79996 79598
79598 78798
78798 79995
79995 79194
79194 77999
77999 80392
80392 78797
78797 79994
79994 78796
78796 79596
79596 77998
77...

result:

wrong output format Extra information in the output file

Test #15:

score: 0
Wrong Answer
time: 26ms
memory: 8312kb

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
160000 783
783 118783
118783 783
783 160000
160000 2921
2921 11721
11721 3171
3171 115019
115019 45735
45735 13002
13002 62202
62202 52742
52742 112742
112742 148342
148342 98342
98342 95542
95542 49142
49142 12908
12908 132350
132350 94350
94350 119962
119962 152587
152587 137999
137999 139599
...

result:

wrong output format Extra information in the output file

Test #16:

score: 0
Wrong Answer
time: 21ms
memory: 8680kb

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
60200 12
12 59999
59999 59799
59799 59995
59995 59599
59599 59993
59993 59197
59197 59399
59399 59798
59798 58598
58598 59992
59992 59595
59595 59796
59796 59991
59991 59195
59195 59990
59990 58597
58597 59989
59989 59594
59594 60199
60199 59193
59193 58999
58999 59795
59795 59987
59987 60198
...

result:

wrong output format Extra information in the output file

Test #17:

score: 0
Wrong Answer
time: 85ms
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
80400 1
1 80399
80399 79999
79999 79599
79599 80398
80398 79598
79598 78799
78799 79597
79597 78399
78399 80396
80396 78798
78798 78398
78398 79997
79997 80395
80395 78397
78397 79595
79595 80394
80394 77999
77999 80393
80393 77599
77599 80392
80392 77199
77199 80391
80391 76799
76799 80390
80...

result:

wrong output format Extra information in the output file

Test #18:

score: 0
Wrong Answer
time: 70ms
memory: 13636kb

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
120000 25
25 119999
119999 116096
116096 119998
119998 119397
119397 119699
119699 119996
119996 118199
118199 119099
119099 119698
119698 118498
118498 119992
119992 118496
118496 119988
119988 119095
119095 118799
118799 119092
119092 119986
119986 119392
119392 119984
119984 119693
119693 1...

result:

wrong output format Extra information in the output file

Test #19:

score: 0
Wrong Answer
time: 148ms
memory: 24572kb

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
132867 1
1 132866
132866 132467
132467 131669
131669 132864
132864 132068
132068 132863
132863 131668
131668 132466
132466 131270
131270 132861
132861 131269
131269 132465
132465 130871
130871 132860
132860 130870
130870 132463
132463 130472
130472 132462
132462 130072
130072 132857
132857 13...

result:

wrong output format Extra information in the output file

Test #20:

score: 0
Wrong Answer
time: 77ms
memory: 16884kb

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
160000 25
25 159999
159999 159196
159196 158799
158799 159998
159998 159593
159593 158398
158398 159194
159194 159591
159591 159997
159997 158797
158797 157998
157998 159590
159590 158397
158397 159996
159996 158794
158794 159995
159995 158396
158396 156399
156399 158793
158793 156398
156398 1...

result:

wrong output format Extra information in the output file