-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Codeforces Gym: 2020-2021 ACM-ICPC Latin American Regional Programmin…
…g Contest
- Loading branch information
Showing
3 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @file 103185C.cpp | ||
* @author Macesuted (i@macesuted.moe) | ||
* @date 2024-09-09 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @file 103185H.cpp | ||
* @author Macesuted (i@macesuted.moe) | ||
* @date 2024-09-09 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Dinic { | ||
private: | ||
struct Edge { | ||
int to, cap, rev; | ||
}; | ||
|
||
vector<vector<Edge>> graph; | ||
vector<vector<Edge>::iterator> cur; | ||
vector<int> dist; | ||
queue<int> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @file 103185L.cpp | ||
* @author Macesuted (i@macesuted.moe) | ||
* @date 2024-09-09 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define maxn 10005 | ||
|
||
typedef pair<int, int> 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; | ||
} |