QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#142680#5280. Depot Rearrangementpenguinman91 36ms24212kbC++174.3kb2023-08-19 17:42:282023-08-19 17:42:29

Judging History

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

  • [2023-08-19 17:42:29]
  • 评测
  • 测评结果:91
  • 用时:36ms
  • 内存:24212kb
  • [2023-08-19 17:42:28]
  • 提交

answer

#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;

#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define ln "\n"
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define mp std::make_pair
#define mtp std::make_tuple

struct union_find{
    ll N;
    vi par, sz;
    union_find(ll n): N(n){
        par.resize(N);
        sz.resize(N,1);
        rep(i,0,N) par[i] = i;
    }
    ll root(ll now){
        if(par[now] == now) return now;
        return par[now] = root(par[now]);
    }
    void merge(ll u, ll v){
        u = root(u);
        v = root(v);
        if(u == v) return;
        if(sz[u] > sz[v]) std::swap(u,v);
        sz[v] += sz[u];
        sz[u] = 0;
        par[u] = v;
    }
    bool same(ll u, ll v){
        return root(u) == root(v);
    }
};


int main(){
    cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    ll N,M; cin >> N >> M;
    vector<vii> data(N,vii(M));
    vi A(N*M);
    rep(i,0,N*M){
        cin >> A[i];
        A[i]--;
        data[i/M][A[i]].pb(i);
    }
    vii edge(N+M), weight(N+M);
    {
        ll cnt = N*M;
        rep(i,0,N){
            rep(j,0,M){
                if(data[i][j].size() == 0){
                    edge[j].pb(M+i);
                    weight[j].pb(cnt++);
                }
                else{
                    rep(k,1,data[i][j].size()){
                        edge[M+i].pb(j);
                        weight[M+i].pb(data[i][j][k]);
                    }
                }
            }
        }
    }
    ll ans = 0;
    {
        vector<bool> flag(N+M);
        rep(i,0,N+M){
            if(edge[i].empty()) continue;
            if(flag[i]) continue;
            flag[i] = true;
            std::queue<ll> que;
            que.push(i);
            ll sum = 0;
            while(!que.empty()){
                ll now = que.front();
                que.pop();
                sum += edge[now].size();
                for(auto next: edge[now]){
                    if(flag[next]) continue;
                    flag[next] = true;
                    que.push(next);
                }
            }
            sum /= 2;
            ans += sum+1;
        }
        cout << ans << ln;
    }
    
    union_find tree(N*M*2);
    vi nidx(N+M);
    vi next(2*N*M, -1);
    rep(i,0,N+M){
        if(nidx[i] == edge[i].size()) continue;
        ll now = i;
        vi ord;
        while(true){
            if(nidx[now] == edge[now].size()) break;
            ll next = edge[now][nidx[now]];
            ll vert = weight[now][nidx[now]];
            ord.pb(vert);
            nidx[now]++;
            now = next;
        }
        rep(j,0,ord.size()){
            next[ord[j]] = ord[(j+1)%ord.size()];
            tree.merge(ord[j], ord[0]);
        }
        assert(nidx[i] == edge[i].size());
    }

    rep(i,0,M+N){
        rep(j,0,weight[i].size()){
            ll n = weight[i][0];
            ll m = weight[i][j];
            if(tree.same(n,m)) continue;
            tree.merge(n,m);
            std::swap(next[n], next[m]);
        }
    }
    ll cnt = 0;
    A.pb(-1);
    rep(i,0,2*N*M){
        if(next[i] == -1) continue;
        vi ord;
        ll now = i;
        while(true){
            if(next[now] == -1) break;
            if(now < N*M) ord.pb(now);
            ll nex = next[now];
            next[now] = -1;
            now = nex;
        }
        ll n = ord.size();
        cout << ord[n-1]+1 << " " << N*M+1 << ln;
        assert(A[N*M] == -1);
        std::swap(A[N*M], A[ord[n-1]]);
        cnt++;
        per(j,n-1,1){
            cout << ord[j-1]+1 << " " << ord[j]+1 << ln;
            assert(A[ord[j]] == -1);
            std::swap(A[ord[j]], A[ord[j-1]]);
            cnt++;
        }
        cout << N*M+1 << " " << ord[0]+1 << ln;
        assert(A[ord[0]] == -1);
        std::swap(A[ord[0]], A[N*M]);
        cnt++;
    }
return 0;
    assert(cnt == ans);
    cout << endl;
    rep(i,0,N){
        vi cnt(M);
        rep(j,i*M,i*M+M) cnt[A[j]]++;
        rep(j,0,M) assert(cnt[j] == 1);
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

result:

ok both subtasks are correct!

Test #3:

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

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

result:

ok both subtasks are correct!

Test #4:

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

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

result:

ok both subtasks are correct!

Test #5:

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

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
13073 20001
2299 13073
15399 2299
6599 15399
1892 6599
3692 1892
13283 3692
18583 13283
6692 18583
3492 6692
15492 3492
3192 15492
8579 3192
9190 8579
10190 9190
9072 10190
1287 9072
8987 1287
11277 8987
8977 11277
8472 8977
12872 8472
18140 12872
11540 18140
7735 11540
8849 7735
7749 8849
17042...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #6:

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

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
84 4021
53 84
37 53
3935 37
3817 3935
3871 3817
3758 3871
3790 3758
3731 3790
3785 3731
3857 3785
3874 3857
3917 3874
3960 3917
3860 3960
3915 3860
3877 3915
3949 3877
3878 3949
3808 3878
3652 3808
3978 3652
3955 3978
3674 3955
3774 3674
3640 3774
3780 3640
3899 3780
3918 3899
3837 3918
3894 38...

result:

ok both subtasks are correct!

Test #7:

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

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
72662 90001
75733 72662
72733 75733
11820 72733
72720 11820
81662 72720
44971 81662
53371 44971
26374 53371
19174 26374
9149 19174
73649 9149
85356 73649
31686 85356
76686 31686
31656 76686
26373 31656
16773 26373
17623 16773
80623 17623
65959 80623
58459 65959
20359 58459
21859 20359
3259 21859...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #8:

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

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
600 12041
12003 600
560 12003
11963 560
520 11963
11923 520
480 11923
11883 480
440 11883
11843 440
400 11843
11803 400
360 11803
11763 360
320 11763
11741 320
10799 11741
11717 10799
10759 11717
11677 10759
10719 11677
11637 10719
10679 11637
11597 10679
10639 11597
11557 10639
10599 11557
11...

result:

ok both subtasks are correct!

Test #9:

score: 5
Accepted
time: 10ms
memory: 8540kb

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
1036 40001
432 1036
649 432
770 649
299 770
39249 299
39626 39249
39022 39626
39775 39022
39674 39775
892 39674
969 892
790 969
1091 790
3556 1091
3951 3556
5189 3951
4965 5189
5490 4965
6744 5490
6129 6744
6471 6129
5651 6471
7631 5651
5598 7631
9959 5598
4796 9959
6083 4796
5485 6083
6388 54...

result:

ok both subtasks are correct!

Test #10:

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

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
353 6401
234 353
839 234
1034 839
2009 1034
977 2009
6226 977
5669 6226
5500 5669
4143 5500
3014 4143
4416 3014
3914 4416
3173 3914
2386 3173
4926 2386
1582 4926
5300 1582
5972 5300
5613 5972
4666 5613
3699 4666
2196 3699
91 2196
1115 91
2634 1115
4868 2634
806 4868
5832 806
1377 5832
583 1377
...

result:

ok both subtasks are correct!

Test #11:

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

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
1484 40001
883 1484
39942 883
39731 39942
39882 39731
39793 39882
39392 39793
39159 39392
36641 39159
36870 36641
38296 36870
36276 38296
38256 36276
34891 38256
34962 34891
36488 34962
38771 36488
38163 38771
34881 38163
37662 34881
37738 37662
34668 37738
38232 34668
38837 38232
39088 38837
...

result:

ok both subtasks are correct!

Test #12:

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

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
5895 6021
640 5895
5863 640
619 5863
5725 619
1098 5725
5709 1098
1080 5709
5684 1080
1060 5684
5663 1060
1039 5663
5642 1039
1020 5642
5624 1020
959 5624
5587 959
940 5587
5567 940
919 5567
5466 919
1440 5466
5188 1440
1520 5188
4989 1520
2397 4989
892 2397
5446 892
1496 5446
4968 1496
2375 49...

result:

ok both subtasks are correct!

Test #13:

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

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
4421 90001
13912 4421
17390 13912
11325 17390
1145 11325
3170 1145
392 3170
88775 392
89562 88775
88035 89562
89492 88035
89080 89492
89663 89080
89906 89663
87217 89906
88478 87217
89622 88478
88628 89622
86697 88628
89254 86697
2607 89254
7473 2607
32866 7473
13174 32866
12447 13174
5559 124...

result:

ok both subtasks are correct!

Test #14:

score: 5
Accepted
time: 18ms
memory: 15380kb

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
404 80401
201 404
48000 201
80319 48000
74369 80319
69183 74369
72376 69183
71585 72376
76759 71585
67992 76759
73971 67992
71181 73971
75963 71181
47569 75963
80318 47569
50666 80318
79928 50666
56400 79928
79138 56400
61998 79138
47096 61998
80317 47096
46765 80317
80316 46765
54800 80316
79...

result:

ok both subtasks are correct!

Test #15:

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

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
118784 160001
784 118784
160001 784
11722 160001
3172 11722
147572 3172
133420 147572
87336 133420
132109 87336
12909 132109
49143 12909
148343 49143
112743 148343
95543 112743
98343 95543
52743 98343
40709 52743
87909 40709
29909 87909
47903 29909
126703 47903
36721 126703
139569 36721
7861 139...

result:

points 0.40 first subtask is correct but plan is wrong.

Test #16:

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

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
2980 60201
232 2980
1234 232
220 1234
59763 220
5374 59763
23232 5374
16796 23232
12300 16796
20486 12300
17142 20486
19659 17142
23705 19659
29730 23705
45362 29730
12122 45362
22219 12122
49919 22219
23992 49919
34577 23992
16845 34577
48767 16845
6520 48767
28905 6520
16642 28905
23667 1664...

result:

ok both subtasks are correct!

Test #17:

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

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
800 80401
80202 800
802 80202
201 802
80204 201
200 80204
79790 200
199 79790
79202 199
198 79202
78800 198
79599 78800
78802 79599
197 78802
21200 197
80253 21200
21202 80253
858 21202
20804 858
857 20804
20404 857
856 20404
20004 856
855 20004
19604 855
854 19604
19204 854
853 19204
6005 853...

result:

ok both subtasks are correct!

Test #18:

score: 5
Accepted
time: 24ms
memory: 18404kb

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
3626 120001
5066 3626
11176 5066
18771 11176
5395 18771
7798 5395
18537 7798
8957 18537
21870 8957
11156 21870
10811 11156
4444 10811
7324 4444
8696 7324
4235 8696
7601 4235
3995 7601
617 3995
3228 617
5032 3228
3628 5032
7041 3628
8546 7041
5610 8546
10349 5610
7355 10349
3906 7355
2749 3906
...

result:

ok both subtasks are correct!

Test #19:

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

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
1197 132868
128540 1197
42617 128540
108666 42617
37448 108666
120190 37448
29097 120190
124960 29097
21529 124960
112240 21529
35460 112240
122185 35460
25109 122185
128144 25109
42616 128144
99921 42616
51791 99921
102696 51791
45010 102696
93952 45010
77244 93952
68090 77244
95143 68090
76...

result:

ok both subtasks are correct!

Test #20:

score: 5
Accepted
time: 27ms
memory: 24212kb

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
158373 160001
157853 158373
155585 157853
156207 155585
153193 156207
153326 153193
157578 153326
152394 157578
151978 152394
152380 151978
138760 152380
154348 138760
153173 154348
153742 153173
153062 153742
159096 153062
159949 159096
1558 159949
5792 1558
17823 5792
34654 17823
13710 34654...

result:

ok both subtasks are correct!