Skip to content

Commit a5ce14d

Browse files
committed
Codeforces Gym: 2020-2021 ACM-ICPC Latin American Regional Programming Contest
1 parent 73985ee commit a5ce14d

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

Codeforces Gym/103185C.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @file 103185C.cpp
3+
* @author Macesuted (i@macesuted.moe)
4+
* @date 2024-09-09
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define maxn 100005
14+
15+
int a[maxn], b[maxn], fa[maxn];
16+
int n;
17+
18+
int getfa(int p) { return fa[p] == p ? p : fa[p] = getfa(fa[p]); }
19+
20+
int64_t calc(void) {
21+
int64_t tot = 0, v = 0;
22+
for (int i = 1; i <= n; i++) b[i] = a[i], v += b[i];
23+
v /= n;
24+
for (int i = 1; i <= n; i++) fa[i] = (b[i] >= v ? i % n + 1 : i);
25+
for (int i = 1; i <= n; i++) {
26+
if (b[i] <= v) continue;
27+
int p = getfa(i), rest = b[i] - v;
28+
b[i] = v;
29+
while (rest && b[p] + rest >= v)
30+
rest -= v - b[p], tot += int64_t(v - b[p]) * ((p - i + n) % n), b[p] = v, p = fa[p] = getfa(p % n + 1);
31+
if (rest) b[p] += rest, tot += rest * ((p - i + n) % n);
32+
}
33+
return tot;
34+
}
35+
36+
void solve(void) {
37+
cin >> n;
38+
for (int i = 1; i <= n; i++) cin >> a[i];
39+
int64_t ans = calc();
40+
reverse(a + 1, a + n + 1);
41+
cout << min(ans, calc()) << endl;
42+
return;
43+
}
44+
45+
int main() {
46+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
47+
48+
int _ = 1;
49+
while (_--) solve();
50+
51+
return 0;
52+
}

Codeforces Gym/103185H.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @file 103185H.cpp
3+
* @author Macesuted (i@macesuted.moe)
4+
* @date 2024-09-09
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
class Dinic {
14+
private:
15+
struct Edge {
16+
int to, cap, rev;
17+
};
18+
19+
vector<vector<Edge>> graph;
20+
vector<vector<Edge>::iterator> cur;
21+
vector<int> dist;
22+
queue<int> que;
23+
int n, S, T;
24+
25+
bool bfs(void) {
26+
for (int i = 1; i <= n; i++) dist[i] = INT_MAX, cur[i] = graph[i].begin();
27+
que.push(S), dist[S] = 0;
28+
while (!que.empty()) {
29+
int p = que.front();
30+
que.pop();
31+
for (auto i : graph[p])
32+
if (i.cap && dist[i.to] > dist[p] + 1) dist[i.to] = dist[p] + 1, que.push(i.to);
33+
}
34+
return dist[T] != INT_MAX;
35+
}
36+
int dfs(int p, int rest) {
37+
if (p == T || !rest) return rest;
38+
int used = 0, c;
39+
for (auto i = cur[p]; i != graph[p].end() && rest; i++) {
40+
cur[p] = i;
41+
if (!i->cap || dist[i->to] != dist[p] + 1) continue;
42+
if (!(c = dfs(i->to, min(rest, i->cap)))) dist[i->to] = -1;
43+
i->cap -= c, graph[i->to][i->rev].cap += c, used += c, rest -= c;
44+
}
45+
return used;
46+
}
47+
48+
public:
49+
void resize(int _n) { return n = _n, graph.resize(n + 1), cur.resize(n + 1), dist.resize(n + 1); }
50+
void addEdge(int from, int to, int cap) {
51+
return graph[from].push_back(Edge{to, cap, (int)graph[to].size()}),
52+
graph[to].push_back(Edge{from, 0, (int)graph[from].size() - 1});
53+
}
54+
int maxFlow(int _S, int _T) {
55+
S = _S, T = _T;
56+
int ans = 0;
57+
while (bfs()) ans += dfs(S, INT_MAX);
58+
return ans;
59+
}
60+
};
61+
62+
void solve(void) {
63+
int n;
64+
cin >> n;
65+
int S = n + 1, T = n + 2;
66+
Dinic dnc;
67+
dnc.resize(T);
68+
for (int i = 1; i <= n; i++) {
69+
string op;
70+
cin >> op;
71+
if (op == "*") {
72+
int v;
73+
cin >> v;
74+
int x = i, y = v;
75+
if (x == 1) x = S;
76+
if (y == 1) y = T;
77+
dnc.addEdge(x, y, 1e8);
78+
} else {
79+
int m = stoi(op);
80+
for (int j = 0, t; j < m; j++) {
81+
cin >> t;
82+
int x = i, y = t;
83+
if (x == 1) x = S;
84+
if (y == 1) y = T;
85+
dnc.addEdge(x, y, 1);
86+
}
87+
}
88+
}
89+
int ans = dnc.maxFlow(S, T);
90+
if (ans >= 1e8) return cout << '*' << endl, void();
91+
cout << ans + 1 << endl;
92+
return;
93+
}
94+
95+
int main() {
96+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
97+
98+
int _ = 1;
99+
while (_--) solve();
100+
101+
return 0;
102+
}

Codeforces Gym/103185L.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file 103185L.cpp
3+
* @author Macesuted (i@macesuted.moe)
4+
* @date 2024-09-09
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define maxn 10005
14+
15+
typedef pair<int, int> pii;
16+
17+
pii a[maxn];
18+
19+
int flr(int x, int y) { return x >= 0 ? x / y : (x / y - (x % y != 0)); }
20+
21+
void solve(void) {
22+
int n, X;
23+
cin >> n >> X;
24+
for (int i = 1, s, d; i <= n; i++) cin >> s >> d, a[i] = {s, s + d};
25+
pii ans = {INT_MAX, INT_MAX};
26+
for (int i = 0; i <= 480; i++) {
27+
int tot = 0;
28+
for (int j = 1; j <= n; j++) {
29+
if (a[j].second < i) continue;
30+
int l = max(-1, flr(a[j].first - 1 - i, X)), r = (a[j].second - i) / X;
31+
tot += r - l;
32+
}
33+
ans = min(ans, pii{tot, i});
34+
}
35+
cout << ans.second << ' ' << ans.first << endl;
36+
return;
37+
}
38+
39+
int main() {
40+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
41+
42+
int _ = 1;
43+
while (_--) solve();
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)