QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#121409 | #1156. Robots | Qwerty1232# | 76 | 670ms | 11156kb | C++17 | 5.0kb | 2023-07-08 03:12:34 | 2024-07-04 00:29:55 |
Judging History
answer
#include "robots.h"
#include <bits/stdc++.h>
// #define assert(exp) \
// if (!(exp)) { \
// exit(0); \
// }
struct Flow {
struct Edge {
int to, id;
int fl, cp;
};
int n, S, T;
std::vector<std::vector<Edge>> gr;
std::vector<int> dist, used;
std::vector<int> dg;
int flow;
void pre_add_edge(int u, int v, int cp) {
dg[u]++;
dg[v]++;
// int a = gr[u].size();
// int b = gr[v].size();
// gr[u].push_back(Edge{v, b, 0, cp});
// gr[v].push_back(Edge{u, a, cp, cp});
}
void init_gr() {
for (int i = 0; i < n; i++) {
gr[i].reserve(dg[i]);
}
}
void add_edge(int u, int v, int cp) {
int a = gr[u].size();
int b = gr[v].size();
gr[u].push_back(Edge{v, b, 0, cp});
gr[v].push_back(Edge{u, a, cp, cp});
}
Flow(int n, int S = 0, int T = 1) : n(n), S(S), T(T), gr(n), dist(n), used(n), flow(0), dg(n) {
;
}
void bfs(int f) {
dist.assign(n, n);
std::deque<int> deque;
deque.push_back(f);
dist[f] = 0;
while (deque.size()) {
int v = deque.front();
deque.pop_front();
for (auto& e : gr[v]) {
if (e.cp != e.fl) {
if (dist[e.to] > dist[v] + 1) {
dist[e.to] = dist[v] + 1;
deque.push_back(e.to);
}
}
}
}
}
void push(Edge& e, int dlt) {
e.fl += dlt;
gr[e.to][e.id].fl -= dlt;
}
int dfs(int v, int min) {
if (v == T || min == 0) {
return min;
}
for (int& i = used[v]; i < gr[v].size(); i++) {
auto& e = gr[v][i];
if (e.fl != e.cp && dist[e.to] == dist[v] + 1) {
int res = dfs(e.to, std::min(min, e.cp - e.fl));
if (res > 0) {
push(e, res);
return res;
}
}
}
return 0;
}
int findMaxFlow() {
bfs(S);
while (dist[T] != n) {
used.assign(n, 0);
int val = 0;
while (val = dfs(S, 1e9)) {
flow += val;
assert(flow < 1e7);
}
bfs(S);
}
return flow;
}
};
int putaway(int n, int m, int q, int x[], int y[], int W[], int S[]) {
std::vector<std::pair<int, int>> toys(q);
for (int i = 0; i < q; i++) {
toys[i] = {W[i], S[i]};
}
// std::vector<int> x(X, X + n);
// std::vector<int> y(Y, Y + m);
std::sort(x, x + n);
std::sort(y, y + m);
// x.erase(std::unique(x.begin(), x.end()), x.end());
// y.erase(std::unique(y.begin(), y.end()), y.end());
for (auto [w, s] : toys) {
if ((n == 0 || w >= x[n - 1]) && (m == 0 || s >= y[m - 1])) {
return -1;
}
}
Flow fl(2 + n + m + q);
for (int i = 0; i < q; i++) {
fl.pre_add_edge(2 + n + m + i, 1, 1);
int it_x = std::lower_bound(x, x + n, toys[i].first + 1) - x;
int it_y = std::lower_bound(y, y + m, toys[i].second + 1) - y;
if (it_x != n)
fl.pre_add_edge(2 + it_x, 2 + n + m + i, 1);
if (it_y != m)
fl.pre_add_edge(2 + n + it_y, 2 + n + m + i, 1);
}
for (int i = 1; i < n; i++) {
fl.pre_add_edge(2 + i, 2 + i - 1, 1e9);
}
for (int i = 1; i < m; i++) {
fl.pre_add_edge(2 + n + i, 2 + n + i - 1, 1e9);
}
for (int i = 0; i < n; i++) {
fl.pre_add_edge(0, 2 + i, 1);
}
for (int i = 0; i < m; i++) {
fl.pre_add_edge(0, 2 + n + i, 1);
}
fl.init_gr();
for (int i = 0; i < q; i++) {
fl.add_edge(2 + n + m + i, 1, 1);
int it_x = std::lower_bound(x, x + n, toys[i].first + 1) - x;
int it_y = std::lower_bound(y, y + m, toys[i].second + 1) - y;
if (it_x != n)
fl.add_edge(2 + it_x, 2 + n + m + i, 1);
if (it_y != m)
fl.add_edge(2 + n + it_y, 2 + n + m + i, 1);
}
for (int i = 1; i < n; i++) {
fl.add_edge(2 + i, 2 + i - 1, 1e9);
}
for (int i = 1; i < m; i++) {
fl.add_edge(2 + n + i, 2 + n + i - 1, 1e9);
}
int ans = 1;
for (int i = 0; i < n; i++) {
fl.add_edge(0, 2 + i, 1);
}
for (int i = 0; i < m; i++) {
fl.add_edge(0, 2 + n + i, 1);
}
while (fl.findMaxFlow() < q) {
// for (int i = 0; i < n; i++) {
// fl.add_edge(0, 2 + i, 1);
// }
// for (int i = 0; i < m; i++) {
// fl.add_edge(0, 2 + n + i, 1);
// }
for (auto& e : fl.gr[0]) {
e.cp++;
fl.gr[e.to][e.id].cp++;
fl.gr[e.to][e.id].fl++;
}
ans++;
assert(ans <= q);
}
return ans;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 14
Accepted
Test #1:
score: 14
Accepted
time: 0ms
memory: 6068kb
input:
1 1 2 13 13 5 6 13 13
output:
-1
result:
ok single line: '-1'
Test #2:
score: 14
Accepted
time: 1ms
memory: 5764kb
input:
0 2 2 20 10 7 10 13 10
output:
2
result:
ok single line: '2'
Test #3:
score: 14
Accepted
time: 0ms
memory: 5996kb
input:
2 0 2 10 20 10 7 10 13
output:
2
result:
ok single line: '2'
Test #4:
score: 14
Accepted
time: 1ms
memory: 5904kb
input:
1 1 2 13 13 15 6 5 16
output:
1
result:
ok single line: '1'
Test #5:
score: 14
Accepted
time: 1ms
memory: 5864kb
input:
0 2 2 15 15 7 8 5 6
output:
1
result:
ok single line: '1'
Test #6:
score: 14
Accepted
time: 1ms
memory: 6068kb
input:
2 0 2 15 15 7 8 5 6
output:
1
result:
ok single line: '1'
Test #7:
score: 14
Accepted
time: 1ms
memory: 5832kb
input:
2 0 2 20 1 10 10 10 10
output:
2
result:
ok single line: '2'
Subtask #2:
score: 0
Time Limit Exceeded
Test #8:
score: 0
Time Limit Exceeded
input:
50000 0 500000 1957680000 64280000 235160000 1384760000 1279320000 1005400000 1481760000 1129920000 1774640000 494160000 763120000 419640000 1742880000 1083000000 278360000 64040000 576880000 1479760000 1872320000 158480000 1183880000 81320000 249920000 30920000 1909240000 870920000 842280000 445640...
output:
result:
Subtask #3:
score: 25
Accepted
Dependency #1:
100%
Accepted
Test #14:
score: 25
Accepted
time: 0ms
memory: 5840kb
input:
3 2 10 6 2 9 4 7 4 6 8 5 2 3 7 9 1 8 5 1 3 3 8 7 7 6 10 5
output:
3
result:
ok single line: '3'
Test #15:
score: 25
Accepted
time: 1ms
memory: 6032kb
input:
2 1 3 2 5 2 3 1 5 3 2 2
output:
-1
result:
ok single line: '-1'
Test #16:
score: 25
Accepted
time: 1ms
memory: 5900kb
input:
25 25 50 18 51 45 57 63 54 30 3 24 21 66 48 6 75 42 72 15 69 39 12 27 9 60 36 33 37 35 13 5 45 43 7 31 11 3 21 1 29 17 15 23 47 41 33 27 9 19 39 49 25 80 662167866 41 854818619 59 750281022 32 713696017 137 1142260087 35 1705242223 65 375567545 107 321944609 68 1918798449 62 1099136337 122 169708110...
output:
-1
result:
ok single line: '-1'
Test #17:
score: 25
Accepted
time: 1ms
memory: 5804kb
input:
49 1 50 63 27 87 54 60 105 147 99 30 45 9 72 48 33 24 51 120 126 36 42 66 141 108 69 78 57 123 3 135 90 93 81 129 111 114 12 75 21 18 132 144 15 102 117 39 138 96 6 84 10 124 15 73 15 67 15 40 15 118 15 112 15 10 15 130 15 28 15 127 15 100 15 16 15 119 5 55 15 121 15 13 15 58 15 139 15 7 15 79 15 64...
output:
2
result:
ok single line: '2'
Test #18:
score: 25
Accepted
time: 0ms
memory: 7944kb
input:
1 49 50 10 81 129 135 78 27 96 42 132 45 144 105 18 93 108 138 102 147 90 87 120 39 3 84 63 6 72 123 54 24 30 117 12 66 114 21 75 126 33 57 36 15 69 9 141 99 51 48 111 60 15 22 15 130 5 80 15 139 15 34 15 127 15 100 15 31 15 1 15 91 15 112 15 73 15 28 15 133 15 106 15 97 15 145 15 61 15 94 15 49 15 ...
output:
2
result:
ok single line: '2'
Test #19:
score: 25
Accepted
time: 1ms
memory: 7864kb
input:
25 25 50 1040000000 560000000 1760000000 1600000000 720000000 240000000 1200000000 1440000000 880000000 320000000 960000000 1120000000 480000000 1920000000 800000000 160000000 400000000 640000000 1680000000 1840000000 1360000000 1520000000 80000000 2000000000 1280000000 1920000000 480000000 10400000...
output:
-1
result:
ok single line: '-1'
Test #20:
score: 25
Accepted
time: 1ms
memory: 5912kb
input:
24 26 50 416666665 1416666661 499999998 1333333328 1083333329 1999999992 1916666659 583333331 999999996 1833333326 166666666 1749999993 249999999 833333330 1666666660 916666663 1583333327 333333332 666666664 749999997 1166666662 83333333 1499999994 1249999995 1307692292 1692307672 1923076900 3076923...
output:
2
result:
ok single line: '2'
Subtask #4:
score: 37
Accepted
Dependency #3:
100%
Accepted
Test #21:
score: 37
Accepted
time: 100ms
memory: 7076kb
input:
500 500 10000 175769951 78234442 1989276125 579229354 1738605419 248650831 1893061510 1407903740 1171874474 1768669963 1239985614 979317232 887530302 529213821 1493976544 567840865 536061097 207463241 290134169 1673628081 1493976544 1823996536 587655611 1673628081 1138738442 675708366 372215090 9304...
output:
11
result:
ok single line: '11'
Test #22:
score: 37
Accepted
time: 670ms
memory: 11156kb
input:
500 500 10000 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 1999907172 ...
output:
10
result:
ok single line: '10'
Test #23:
score: 37
Accepted
time: 1ms
memory: 6096kb
input:
50 50 50 51 7 77 65 57 85 13 83 9 71 87 47 31 89 43 99 61 5 53 95 55 39 45 93 73 81 1 29 15 37 49 3 27 91 35 21 41 67 69 33 25 97 75 79 63 19 17 23 11 59 93 111 108 54 96 114 45 3 129 6 105 69 84 99 81 21 63 123 141 126 27 87 147 36 42 78 117 9 144 72 60 18 138 135 15 30 48 57 75 150 12 51 66 102 13...
output:
2
result:
ok single line: '2'
Test #24:
score: 37
Accepted
time: 14ms
memory: 7216kb
input:
512 488 10000 281250000 562500000 1753906250 1312500000 667968750 1738281250 207031250 195312500 1667968750 550781250 1500000000 1382812500 652343750 300781250 488281250 1613281250 156250000 183593750 1789062500 468750000 1300781250 1792968750 375000000 1882812500 1710937500 1363281250 574218750 132...
output:
10
result:
ok single line: '10'
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%