From b3fcbc9abd7bfca6b51d86d2ec291c43060a5f22 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 24 Mar 2022 20:52:37 +0800 Subject: [PATCH] =?UTF-8?q?#6013.=20=E3=80=8C=E7=BD=91=E7=BB=9C=E6=B5=81?= =?UTF-8?q?=2024=20=E9=A2=98=E3=80=8D=E8=B4=9F=E8=BD=BD=E5=B9=B3=E8=A1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://loj.ac/s/1421881 --- LibreOJ/6013/6013.cpp | 131 +++++++++++++++++++++++++++++++++++ LibreOJ/6013/data/move0.in | 3 + LibreOJ/6013/data/move0.out | 3 + LibreOJ/6013/data/move1.in | 3 + LibreOJ/6013/data/move1.out | 3 + LibreOJ/6013/data/move10.in | 3 + LibreOJ/6013/data/move10.out | 3 + LibreOJ/6013/data/move2.in | 3 + LibreOJ/6013/data/move2.out | 3 + LibreOJ/6013/data/move3.in | 3 + LibreOJ/6013/data/move3.out | 3 + LibreOJ/6013/data/move4.in | 3 + LibreOJ/6013/data/move4.out | 3 + LibreOJ/6013/data/move5.in | 3 + LibreOJ/6013/data/move5.out | 3 + LibreOJ/6013/data/move6.in | 3 + LibreOJ/6013/data/move6.out | 3 + LibreOJ/6013/data/move7.in | 3 + LibreOJ/6013/data/move7.out | 3 + LibreOJ/6013/data/move8.in | 3 + LibreOJ/6013/data/move8.out | 3 + LibreOJ/6013/data/move9.in | 3 + LibreOJ/6013/data/move9.out | 3 + 23 files changed, 197 insertions(+) create mode 100644 LibreOJ/6013/6013.cpp create mode 100644 LibreOJ/6013/data/move0.in create mode 100644 LibreOJ/6013/data/move0.out create mode 100644 LibreOJ/6013/data/move1.in create mode 100644 LibreOJ/6013/data/move1.out create mode 100644 LibreOJ/6013/data/move10.in create mode 100644 LibreOJ/6013/data/move10.out create mode 100644 LibreOJ/6013/data/move2.in create mode 100644 LibreOJ/6013/data/move2.out create mode 100644 LibreOJ/6013/data/move3.in create mode 100644 LibreOJ/6013/data/move3.out create mode 100644 LibreOJ/6013/data/move4.in create mode 100644 LibreOJ/6013/data/move4.out create mode 100644 LibreOJ/6013/data/move5.in create mode 100644 LibreOJ/6013/data/move5.out create mode 100644 LibreOJ/6013/data/move6.in create mode 100644 LibreOJ/6013/data/move6.out create mode 100644 LibreOJ/6013/data/move7.in create mode 100644 LibreOJ/6013/data/move7.out create mode 100644 LibreOJ/6013/data/move8.in create mode 100644 LibreOJ/6013/data/move8.out create mode 100644 LibreOJ/6013/data/move9.in create mode 100644 LibreOJ/6013/data/move9.out diff --git a/LibreOJ/6013/6013.cpp b/LibreOJ/6013/6013.cpp new file mode 100644 index 00000000..e0474526 --- /dev/null +++ b/LibreOJ/6013/6013.cpp @@ -0,0 +1,131 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 105, + M = 2005, + INF = 0x3f3f3f3f; + +int n, s, t, a[N], sum, avg, cost; + +// Graph +int idx, head[N], ver[M << 1], next[M << 1]; +std::pair edge[M << 1]; +// + +void add(int u, int v, int flow, int cost) { + next[idx] = head[u]; + ver[idx] = v; + edge[idx] = std::make_pair(flow, cost); + head[u] = idx++; +} + +// Dinic +int dist[N]; +bool vis[N]; + +bool spfa() { + std::memset(vis, 0x00, sizeof(vis)); + std::memset(dist, 0x3f, sizeof(dist)); + + std::queue q; + q.push(s); + dist[s] = 0; + vis[s] = true; + + while (!q.empty()) { + int u = q.front(); + q.pop(); + vis[u] = false; + + for (int i = head[u]; ~i; i = next[i]) { + int v = ver[i], + c = edge[i].first, + w = edge[i].second; + + if (dist[v] > dist[u] + w && c > 0) { + dist[v] = dist[u] + w; + if (!vis[v]) { + q.push(v); + vis[v] = true; + } + } + } + } + + return dist[t] != INF; +} + +int dinic(int u, int limit) { + if (u == t) return limit; + + int flow = 0; + vis[u] = true; + for (int i = head[u]; ~i && flow < limit; i = next[i]) { + int v = ver[i], + c = edge[i].first, + w = edge[i].second; + + if (dist[v] == dist[u] + w && c && !vis[v]) { + int k = dinic(v, std::min(c, limit - flow)); + if (!k) dist[v] = INF; + edge[i].first -= k; + edge[i ^ 1].first += k; + flow += k; + cost += k * w; + } + } + vis[u] = false; + + return flow; +} + +int main() { + std::ios::sync_with_stdio(false); + + memset(head, 0xff, sizeof(head)); + + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> a[i]; + sum += a[i]; + } + avg = sum / n; + + s = 0, t = n + 1; + for (int i = 1; i <= n; i++) { + if (a[i] - avg > 0) { + add(s, i, a[i] - avg, 0); + add(i, s, 0, 0); + } else if (a[i] - avg < 0) { + add(i, t, avg - a[i], 0); + add(t, i, 0, 0); + } + } + + for (int i = 1; i <= n; i++) { + if (i > 1) { + add(i, i - 1, INF, 1); + add(i - 1, i, 0, -1); + } + if (i < n) { + add(i, i + 1, INF, 1); + add(i + 1, i, 0, -1); + } + } + + add(1, n, INF, 1); + add(n, 1, 0, -1); + add(n, 1, INF, 1); + add(1, n, 0, -1); + + while (spfa()) dinic(s, INF); + + cout << cost << endl; + + return 0; +} diff --git a/LibreOJ/6013/data/move0.in b/LibreOJ/6013/data/move0.in new file mode 100644 index 00000000..69b35a47 --- /dev/null +++ b/LibreOJ/6013/data/move0.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c836174e47a2bc4aa8f6f6b3257ff083713b81bf89da11cabf037c0741391642 +size 15 diff --git a/LibreOJ/6013/data/move0.out b/LibreOJ/6013/data/move0.out new file mode 100644 index 00000000..4266d343 --- /dev/null +++ b/LibreOJ/6013/data/move0.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:412a8da13330793d63a3a17bed32c76be361edad27fe0f6a0ceb144d3f0aa50e +size 4 diff --git a/LibreOJ/6013/data/move1.in b/LibreOJ/6013/data/move1.in new file mode 100644 index 00000000..12ce1d4c --- /dev/null +++ b/LibreOJ/6013/data/move1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15eeee8b73310c86d3b16da568d3adfa39036e6fe81561265db857d2892d0a79 +size 69 diff --git a/LibreOJ/6013/data/move1.out b/LibreOJ/6013/data/move1.out new file mode 100644 index 00000000..5d5b3d04 --- /dev/null +++ b/LibreOJ/6013/data/move1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af54d0238f3215701c1088970b4b8fc5b22b4dc8b92d471240e6845d602d4613 +size 5 diff --git a/LibreOJ/6013/data/move10.in b/LibreOJ/6013/data/move10.in new file mode 100644 index 00000000..fe665435 --- /dev/null +++ b/LibreOJ/6013/data/move10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b6a34a22ce5e125e3f0717ff649ad11c76d4635088707769dc2aa7ab06e04eb +size 307 diff --git a/LibreOJ/6013/data/move10.out b/LibreOJ/6013/data/move10.out new file mode 100644 index 00000000..2f2314d8 --- /dev/null +++ b/LibreOJ/6013/data/move10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:35ad847826e80782bc9b81e419bedb997f452fd5fe7a7b59f5874c6420a3168e +size 6 diff --git a/LibreOJ/6013/data/move2.in b/LibreOJ/6013/data/move2.in new file mode 100644 index 00000000..aeb72b7a --- /dev/null +++ b/LibreOJ/6013/data/move2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:068cbde5a4df8d34d70cd6f97259553007de2feea14c5af2074f085f8a3406c4 +size 78 diff --git a/LibreOJ/6013/data/move2.out b/LibreOJ/6013/data/move2.out new file mode 100644 index 00000000..027efe92 --- /dev/null +++ b/LibreOJ/6013/data/move2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4562d0c58580c5bcb9ee15f400c042b7a5c3aac4522d49e32421ea9eb16dd673 +size 5 diff --git a/LibreOJ/6013/data/move3.in b/LibreOJ/6013/data/move3.in new file mode 100644 index 00000000..aef32b02 --- /dev/null +++ b/LibreOJ/6013/data/move3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4c197fb1b15b74356ca48f16d31ded1c0a81d6ce00da6a7fcb9d91a91261fc86 +size 181 diff --git a/LibreOJ/6013/data/move3.out b/LibreOJ/6013/data/move3.out new file mode 100644 index 00000000..fe585dd9 --- /dev/null +++ b/LibreOJ/6013/data/move3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bb0fccde4a05d8f9c7be84c45c6cd72328b29a03dea54adcc851a2b6430bdcf3 +size 6 diff --git a/LibreOJ/6013/data/move4.in b/LibreOJ/6013/data/move4.in new file mode 100644 index 00000000..a2620ed7 --- /dev/null +++ b/LibreOJ/6013/data/move4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:95f87274622c049450adc3939911f08a24f0836e25a8020d716dd402136daaa6 +size 226 diff --git a/LibreOJ/6013/data/move4.out b/LibreOJ/6013/data/move4.out new file mode 100644 index 00000000..97bd1489 --- /dev/null +++ b/LibreOJ/6013/data/move4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a456008fc52710e02912110429a3d90e51e32f24868a1628289310dd23d3d52a +size 6 diff --git a/LibreOJ/6013/data/move5.in b/LibreOJ/6013/data/move5.in new file mode 100644 index 00000000..f1c4fb85 --- /dev/null +++ b/LibreOJ/6013/data/move5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:68541ddd078c0fc06895d6f065c6359e4d5fbc14015977fd922c4904c42c3404 +size 106 diff --git a/LibreOJ/6013/data/move5.out b/LibreOJ/6013/data/move5.out new file mode 100644 index 00000000..d545e59a --- /dev/null +++ b/LibreOJ/6013/data/move5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e5cc5b8f7a259e8ea249c613e786f692e4d1e22dd841da11c29b2eed683c157e +size 5 diff --git a/LibreOJ/6013/data/move6.in b/LibreOJ/6013/data/move6.in new file mode 100644 index 00000000..67dd9a59 --- /dev/null +++ b/LibreOJ/6013/data/move6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1aae4287911244bd9c63e853b95c6b36bb42b9e7a23b09e433973632148008a5 +size 55 diff --git a/LibreOJ/6013/data/move6.out b/LibreOJ/6013/data/move6.out new file mode 100644 index 00000000..e6bc43ea --- /dev/null +++ b/LibreOJ/6013/data/move6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1a8dffc98fe89ee2e2c6aae1531ddae847a0bab8a810c6305c9d067eaa501acc +size 5 diff --git a/LibreOJ/6013/data/move7.in b/LibreOJ/6013/data/move7.in new file mode 100644 index 00000000..ec42981d --- /dev/null +++ b/LibreOJ/6013/data/move7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a36ec6daa8160be18fd2986aaa2c085f1ac8600b799e3922ca7604202a2a4c8a +size 106 diff --git a/LibreOJ/6013/data/move7.out b/LibreOJ/6013/data/move7.out new file mode 100644 index 00000000..66550ae4 --- /dev/null +++ b/LibreOJ/6013/data/move7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:615342ddd5d87fda74ed39926815fd0f5eb23f59df88bbe2ce1e414e79f4d90b +size 5 diff --git a/LibreOJ/6013/data/move8.in b/LibreOJ/6013/data/move8.in new file mode 100644 index 00000000..fdb303db --- /dev/null +++ b/LibreOJ/6013/data/move8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:02f289183427bcddc6625ae77fdf620f164610a4ed5c3bf742b2acb9d9aa0891 +size 213 diff --git a/LibreOJ/6013/data/move8.out b/LibreOJ/6013/data/move8.out new file mode 100644 index 00000000..2ce3c413 --- /dev/null +++ b/LibreOJ/6013/data/move8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1dc38ab87f90bdb10db57e8822342627053dc3fbc9f63d0abe25a92ec9ad5700 +size 6 diff --git a/LibreOJ/6013/data/move9.in b/LibreOJ/6013/data/move9.in new file mode 100644 index 00000000..94dbf9e9 --- /dev/null +++ b/LibreOJ/6013/data/move9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ff8336bdc46075c3364af849478e585b2db975f42be91f1d13d32a1cee94790b +size 141 diff --git a/LibreOJ/6013/data/move9.out b/LibreOJ/6013/data/move9.out new file mode 100644 index 00000000..c84fadec --- /dev/null +++ b/LibreOJ/6013/data/move9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b890c2dd1de0f67228fb53773353d2f39be5a5dd7cb218439849f8f88f100330 +size 6