QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#141416 | #5280. Depot Rearrangement | penguinman# | 91 | 26ms | 23596kb | C++17 | 3.9kb | 2023-08-17 11:50:07 | 2024-07-04 02:39:13 |
Judging History
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[(j+1)%ord.size()]);
}
}
rep(i,0,M+N){
rep(j,1,weight[i].size()){
ll n = weight[i][j-1];
ll m = weight[i][j];
if(tree.same(n,m)) continue;
tree.merge(n,m);
std::swap(next[n], next[m]);
}
}
ll cnt = 0;
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;
cnt++;
per(j,n-1,1){
cout << ord[j-1]+1 << " " << ord[j]+1 << ln;
cnt++;
}
cout << N*M+1 << " " << ord[0]+1 << ln;
cnt++;
}
assert(cnt == ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 0ms
memory: 3620kb
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: 0ms
memory: 3540kb
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 12 7 20 12 15 20 10 15 4 10 18 4 3 18 14 3 2 14 21 2
result:
ok both subtasks are correct!
Test #3:
score: 5
Accepted
time: 0ms
memory: 3612kb
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 6 29 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 76 43 66 76 20 66 3 20 101 3
result:
ok both subtasks are correct!
Test #4:
score: 5
Accepted
time: 0ms
memory: 3736kb
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: 0ms
memory: 5460kb
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 3492 1892 15492 3492 3692 15492 13283 3692 18583 13283 6692 18583 3192 6692 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 12459 7735 6084 12459 118...
result:
points 0.40 first subtask is correct but plan is wrong.
Test #6:
score: 5
Accepted
time: 0ms
memory: 4084kb
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 3940 3860 3914 3940 3966 3914 3915 3966 3877 3915 4000 3877 3872 4000 3913 3872 3949 3913 3878 3949 3808 3878 3652 3808 3999 3652 3980 3999 3832 3980 3898 38...
result:
ok both subtasks are correct!
Test #7:
score: 2
Acceptable Answer
time: 5ms
memory: 13188kb
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 3259 58459 77959 3259 20359 77959 ...
result:
points 0.40 first subtask is correct but plan is wrong.
Test #8:
score: 5
Accepted
time: 3ms
memory: 5548kb
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 322 12041 280 322 12002 280 240 12002 11962 240 200 11962 11922 200 160 11922 11882 160 120 11882 11842 120 80 11842 11802 80 40 11802 11762 40 301 11762 11722 301 279 11722 11682 279 239 11682 11642 239 199 11642 11602 199 159 11602 11562 159 119 11562 11522 119 79 11522 11482 79 39 11482 114...
result:
ok both subtasks are correct!
Test #9:
score: 5
Accepted
time: 4ms
memory: 8596kb
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 892 299 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 5485 4256 6388 3292 4256 4081 3292 4563 4081 5342 4563 9639 53...
result:
ok both subtasks are correct!
Test #10:
score: 5
Accepted
time: 0ms
memory: 4428kb
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 806 977 182 806 40 182 338 40 137 338 6047 137 5914 6047 3700 5914 5370 3700 5035 5370 4444 5035 5472 4444 5232 5472 3792 5232 3572 3792 2999 3572 2632 2999 1504 2632 945 1504 2399 945 2468 2399 914 2468 424 914 1996 424 1437 1996 4664 1437 6...
result:
ok both subtasks are correct!
Test #11:
score: 5
Accepted
time: 6ms
memory: 8472kb
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 382 883 1461 382 1264 1461 2866 1264 1668 2866 897 1668 2557 897 2395 2557 3182 2395 1477 3182 1299 1477 2448 1299 1069 2448 779 1069 1941 779 3376 1941 4765 3376 3397 4765 4580 3397 3353 4580 2999 3353 2872 2999 2398 2872 1456 2398 788 1456 642 788 1061 642 585 1061 996 58...
result:
ok both subtasks are correct!
Test #12:
score: 5
Accepted
time: 0ms
memory: 4496kb
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 322 6021 5932 322 5895 5932 780 5895 6007 780 760 6007 5982 760 740 5982 2765 740 4591 2765 2754 4591 4574 2754 2738 4574 4551 2738 2714 4551 4729 2714 2697 4729 4709 2697 2655 4709 4687 2655 2636 4687 4671 2636 2595 4671 4650 2595 2554 4650 4630 2554 2535 4630 4610 2535 2495 4610 4589 2495 247...
result:
ok both subtasks are correct!
Test #13:
score: 5
Accepted
time: 16ms
memory: 14120kb
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 2607 392 7473 2607 32866 7473 13174 32866 12447 13174 5559 12447 3751 5559 9214 3751 9556 9214 17366 9556 23040 17366 30677 23040 22415 30677 25339 22415 30586 25339 44580 30586 51536 44580 49754 51536 41341 49754 4729...
result:
ok both subtasks are correct!
Test #14:
score: 5
Accepted
time: 21ms
memory: 15228kb
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 48002 201 2129 48002 66199 2129 966 66199 52389 966 1733 52389 38980 1733 3705 38980 38701 3705 3321 38701 25208 3321 12461 25208 8471 12461 20821 8471 66341 20821 571 66341 51948 571 1732 51948 38009 1732 3319 38009 10060 3319 18025 10060 16847 18025 16430 16847 17639 16430 ...
result:
ok both subtasks are correct!
Test #15:
score: 2
Acceptable Answer
time: 6ms
memory: 20732kb
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 95543 49143 98343 95543 148343 98343 112743 148343 52743 112743 29909 52743 47903 29909 126703 47903 36721 126703 139569 36721 7861 139569 33061 7861 133788 33...
result:
points 0.40 first subtask is correct but plan is wrong.
Test #16:
score: 5
Accepted
time: 9ms
memory: 10820kb
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 621 220 56598 621 58236 56598 57713 58236 57913 57713 39072 57913 32992 39072 35789 32992 14077 35789 26388 14077 15344 26388 37483 15344 40914 37483 8795 40914 3073 8795 22380 3073 7434 22380 22487 7434 44930 22487 7110 44930 22469 7110 15766 22469 32735 ...
result:
ok both subtasks are correct!
Test #17:
score: 5
Accepted
time: 20ms
memory: 15928kb
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 802 80401 201 802 80204 201 200 80204 79790 200 199 79790 79202 199 198 79202 78802 198 197 78802 21202 197 858 21202 20804 858 857 20804 20404 857 856 20404 20004 856 855 20004 19604 855 854 19604 19204 854 853 19204 6005 853 1222 6005 2015 1222 4806 2015 3611 4806 1619 3611 5604 1619 1221 56...
result:
ok both subtasks are correct!
Test #18:
score: 5
Accepted
time: 19ms
memory: 18140kb
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: 26ms
memory: 23596kb
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 1199 132868 800 1199 401 800 333 401 96572 333 3094 96572 76262 3094 18174 76262 78646 18174 9034 78646 88606 9034 16983 88606 66314 16983 24134 66314 71485 24134 31296 71485 65913 31296 24133 65913 71877 24133 13820 71877 57959 13820 23756 57959 71876 23756 28504 71876 69894 28504 13410 6989...
result:
ok both subtasks are correct!
Test #20:
score: 5
Accepted
time: 23ms
memory: 23404kb
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 1558 160001 5792 1558 17823 5792 34654 17823 13710 34654 20196 13710 35858 20196 35168 35858 57789 35168 53914 57789 55388 53914 34279 55388 56977 34279 37527 56977 40364 37527 60698 40364 94562 60698 73413 94562 59768 73413 69774 59768 60249 69774 70129 60249 85041 70129 47997 85041 53588 479...
result:
ok both subtasks are correct!