diff --git a/Codeforces Gym/103185C.cpp b/Codeforces Gym/103185C.cpp new file mode 100644 index 0000000..bf86e11 --- /dev/null +++ b/Codeforces Gym/103185C.cpp @@ -0,0 +1,52 @@ +/** + * @file 103185C.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-09-09 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define maxn 100005 + +int a[maxn], b[maxn], fa[maxn]; +int n; + +int getfa(int p) { return fa[p] == p ? p : fa[p] = getfa(fa[p]); } + +int64_t calc(void) { + int64_t tot = 0, v = 0; + for (int i = 1; i <= n; i++) b[i] = a[i], v += b[i]; + v /= n; + for (int i = 1; i <= n; i++) fa[i] = (b[i] >= v ? i % n + 1 : i); + for (int i = 1; i <= n; i++) { + if (b[i] <= v) continue; + int p = getfa(i), rest = b[i] - v; + b[i] = v; + while (rest && b[p] + rest >= v) + rest -= v - b[p], tot += int64_t(v - b[p]) * ((p - i + n) % n), b[p] = v, p = fa[p] = getfa(p % n + 1); + if (rest) b[p] += rest, tot += rest * ((p - i + n) % n); + } + return tot; +} + +void solve(void) { + cin >> n; + for (int i = 1; i <= n; i++) cin >> a[i]; + int64_t ans = calc(); + reverse(a + 1, a + n + 1); + cout << min(ans, calc()) << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + int _ = 1; + while (_--) solve(); + + return 0; +} \ No newline at end of file diff --git a/Codeforces Gym/103185H.cpp b/Codeforces Gym/103185H.cpp new file mode 100644 index 0000000..c35e61b --- /dev/null +++ b/Codeforces Gym/103185H.cpp @@ -0,0 +1,102 @@ +/** + * @file 103185H.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-09-09 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +class Dinic { + private: + struct Edge { + int to, cap, rev; + }; + + vector> graph; + vector::iterator> cur; + vector dist; + queue que; + int n, S, T; + + bool bfs(void) { + for (int i = 1; i <= n; i++) dist[i] = INT_MAX, cur[i] = graph[i].begin(); + que.push(S), dist[S] = 0; + while (!que.empty()) { + int p = que.front(); + que.pop(); + for (auto i : graph[p]) + if (i.cap && dist[i.to] > dist[p] + 1) dist[i.to] = dist[p] + 1, que.push(i.to); + } + return dist[T] != INT_MAX; + } + int dfs(int p, int rest) { + if (p == T || !rest) return rest; + int used = 0, c; + for (auto i = cur[p]; i != graph[p].end() && rest; i++) { + cur[p] = i; + if (!i->cap || dist[i->to] != dist[p] + 1) continue; + if (!(c = dfs(i->to, min(rest, i->cap)))) dist[i->to] = -1; + i->cap -= c, graph[i->to][i->rev].cap += c, used += c, rest -= c; + } + return used; + } + + public: + void resize(int _n) { return n = _n, graph.resize(n + 1), cur.resize(n + 1), dist.resize(n + 1); } + void addEdge(int from, int to, int cap) { + return graph[from].push_back(Edge{to, cap, (int)graph[to].size()}), + graph[to].push_back(Edge{from, 0, (int)graph[from].size() - 1}); + } + int maxFlow(int _S, int _T) { + S = _S, T = _T; + int ans = 0; + while (bfs()) ans += dfs(S, INT_MAX); + return ans; + } +}; + +void solve(void) { + int n; + cin >> n; + int S = n + 1, T = n + 2; + Dinic dnc; + dnc.resize(T); + for (int i = 1; i <= n; i++) { + string op; + cin >> op; + if (op == "*") { + int v; + cin >> v; + int x = i, y = v; + if (x == 1) x = S; + if (y == 1) y = T; + dnc.addEdge(x, y, 1e8); + } else { + int m = stoi(op); + for (int j = 0, t; j < m; j++) { + cin >> t; + int x = i, y = t; + if (x == 1) x = S; + if (y == 1) y = T; + dnc.addEdge(x, y, 1); + } + } + } + int ans = dnc.maxFlow(S, T); + if (ans >= 1e8) return cout << '*' << endl, void(); + cout << ans + 1 << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + int _ = 1; + while (_--) solve(); + + return 0; +} \ No newline at end of file diff --git a/Codeforces Gym/103185L.cpp b/Codeforces Gym/103185L.cpp new file mode 100644 index 0000000..69aeb24 --- /dev/null +++ b/Codeforces Gym/103185L.cpp @@ -0,0 +1,46 @@ +/** + * @file 103185L.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-09-09 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define maxn 10005 + +typedef pair pii; + +pii a[maxn]; + +int flr(int x, int y) { return x >= 0 ? x / y : (x / y - (x % y != 0)); } + +void solve(void) { + int n, X; + cin >> n >> X; + for (int i = 1, s, d; i <= n; i++) cin >> s >> d, a[i] = {s, s + d}; + pii ans = {INT_MAX, INT_MAX}; + for (int i = 0; i <= 480; i++) { + int tot = 0; + for (int j = 1; j <= n; j++) { + if (a[j].second < i) continue; + int l = max(-1, flr(a[j].first - 1 - i, X)), r = (a[j].second - i) / X; + tot += r - l; + } + ans = min(ans, pii{tot, i}); + } + cout << ans.second << ' ' << ans.first << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + int _ = 1; + while (_--) solve(); + + return 0; +} \ No newline at end of file