Skip to content

Commit 8f1e22d

Browse files
committed
Codeforces Gym: 2003-2004 Summer Petrozavodsk Camp, Andrew Stankevich Contest 2 (ASC 2)
1 parent 75ed33a commit 8f1e22d

File tree

5 files changed

+402
-0
lines changed

5 files changed

+402
-0
lines changed

Codeforces Gym/100197A.cpp

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @file 100197A.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-01-10
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
#define maxn 65
20+
#define maxk 1005
21+
22+
typedef vector<int8_t> bigInt;
23+
24+
int jmp[maxk][30], inq[maxk];
25+
bool tgt[maxk], cont[maxk][30], vis[maxk];
26+
bigInt f[maxn][maxk];
27+
28+
void add(bigInt& a, const bigInt& b) {
29+
if (a.size() < b.size()) a.resize(b.size());
30+
for (int i = 0; i < (int)b.size(); i++) a[i] += b[i];
31+
for (int i = 0; i < (int)a.size(); i++) {
32+
if (a[i] < 10) continue;
33+
if (i + 1 == (int)a.size()) a.push_back(0);
34+
a[i + 1]++, a[i] -= 10;
35+
}
36+
return;
37+
}
38+
39+
void solve(void) {
40+
string sigma;
41+
cin >> sigma;
42+
int m = sigma.size();
43+
44+
int k;
45+
cin >> k;
46+
47+
int S, Tcnt;
48+
cin >> S >> Tcnt;
49+
for (int t = 1, x; t <= Tcnt; t++) cin >> x, tgt[x] = true;
50+
51+
for (int i = 1; i <= k; i++)
52+
for (int j = 1; j <= m; j++) cin >> jmp[i][j];
53+
for (int i = 1; i <= k; i++)
54+
for (int j = 1; j <= m; j++) cin >> cont[i][j];
55+
for (int j = 1; j <= m; j++) {
56+
for (int i = 1; i <= k; i++) vis[i] = !cont[i][j];
57+
for (int i = 1; i <= k; i++) {
58+
if (vis[i]) continue;
59+
stack<int> S;
60+
S.push(i), inq[i]++;
61+
while (!vis[S.top()] && inq[S.top()] == 1) S.push(jmp[S.top()][j]), inq[S.top()]++;
62+
if (!vis[S.top()]) jmp[S.top()][j] = 0;
63+
int ans = jmp[S.top()][j];
64+
while (!S.empty()) inq[S.top()]--, jmp[S.top()][j] = ans, vis[S.top()] = true, S.pop();
65+
}
66+
}
67+
68+
int n;
69+
cin >> n;
70+
71+
f[0][S].push_back(1);
72+
73+
for (int i = 1; i <= n; i++)
74+
for (int j = 1; j <= k; j++) {
75+
if (f[i - 1][j].empty()) continue;
76+
for (int c = 1; c <= m; c++)
77+
if (jmp[j][c]) add(f[i][jmp[j][c]], f[i - 1][j]);
78+
}
79+
80+
bigInt ans;
81+
ans.push_back(0);
82+
for (int i = 1; i <= k; i++)
83+
if (tgt[i]) add(ans, f[n][i]);
84+
85+
reverse(ans.begin(), ans.end());
86+
for (auto x : ans) cout << (int)x;
87+
cout << endl;
88+
89+
return;
90+
}
91+
92+
bool mem2;
93+
94+
int main() {
95+
#ifndef LOCAL
96+
freopen("dfa.in", "r", stdin), freopen("dfa.out", "w", stdout);
97+
#endif
98+
ios::sync_with_stdio(false), cin.tie(nullptr);
99+
#ifdef LOCAL
100+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
101+
#endif
102+
103+
int _ = 1;
104+
while (_--) solve();
105+
106+
#ifdef LOCAL
107+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
108+
#endif
109+
return 0;
110+
}

Codeforces Gym/100197B.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @file 100197B.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-01-10
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
#define maxn 70
20+
21+
int f[maxn][maxn], g[maxn][maxn], pre[maxn][maxn];
22+
23+
stack<int> S[maxn];
24+
vector<string> ans;
25+
26+
void mov(int x, int y) {
27+
int v = S[x].top();
28+
S[x].pop();
29+
ans.push_back("move " + to_string(v) + " from " + to_string(x) + " to " + to_string(y));
30+
if (!S[y].empty()) ans.back() += " atop " + to_string(S[y].top());
31+
S[y].push(v);
32+
return;
33+
}
34+
35+
void solve(int m, int n, int from, int to, vector<int> pos) {
36+
vector<int> x(m + 1);
37+
for (int i = m; i >= 3; i--) x[i] = pre[i][n - 1], n -= x[i];
38+
39+
for (int i = m; i >= 3; i--) {
40+
if (!x[i]) continue;
41+
vector<int> npos = pos;
42+
npos.resize(i - 3), npos.push_back(to);
43+
solve(i, x[i], from, pos[i - 3], npos);
44+
}
45+
46+
mov(from, to);
47+
48+
for (int i = 3; i <= m; i++) {
49+
if (!x[i]) continue;
50+
vector<int> npos = pos;
51+
npos.resize(i - 3), npos.push_back(from);
52+
solve(i, x[i], pos[i - 3], to, npos);
53+
}
54+
55+
return;
56+
}
57+
58+
void solve(void) {
59+
int n, m;
60+
cin >> n >> m;
61+
62+
for (int i = 1; i <= m; i++)
63+
for (int j = 1; j <= n; j++) f[i][j] = g[i][j] = 1e9;
64+
f[2][1] = 1;
65+
66+
for (int i = 3; i <= m; i++)
67+
for (int j = 1; j <= n; j++) {
68+
f[i][j] = min(int(1e9), 1 + 2 * g[i][j - 1]);
69+
for (int k = j; k <= n; k++)
70+
if (g[i][k] > g[i - 1][k - j] + f[i][j]) g[i][k] = g[i - 1][k - j] + f[i][j], pre[i][k] = j;
71+
}
72+
73+
vector<int> pos;
74+
for (int i = 2; i < m; i++) pos.push_back(i);
75+
for (int i = n; i; i--) S[1].push(i);
76+
solve(m, n, 1, m, pos);
77+
78+
cout << ans.size() << endl;
79+
for (auto x : ans) cout << x << endl;
80+
81+
return;
82+
}
83+
84+
bool mem2;
85+
86+
int main() {
87+
#ifndef LOCAL
88+
freopen("hanoi.in", "r", stdin), freopen("hanoi.out", "w", stdout);
89+
#endif
90+
91+
ios::sync_with_stdio(false), cin.tie(nullptr);
92+
#ifdef LOCAL
93+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
94+
#endif
95+
96+
int _ = 1;
97+
while (_--) solve();
98+
99+
#ifdef LOCAL
100+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
101+
#endif
102+
return 0;
103+
}

Codeforces Gym/100197C.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @file 100197C.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-01-10
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
void solve(void) {
20+
int n;
21+
cin >> n;
22+
priority_queue<int64_t, vector<int64_t>, greater<int64_t>> que;
23+
int64_t ans = 0;
24+
for (int i = 1, x; i <= n; i++) cin >> x, que.push(x);
25+
while (que.size() > 1) {
26+
int64_t p = que.top();
27+
que.pop();
28+
int64_t q = que.top();
29+
que.pop();
30+
ans += p + q;
31+
que.push(p + q);
32+
}
33+
cout << ans << endl;
34+
return;
35+
}
36+
37+
bool mem2;
38+
39+
int main() {
40+
#ifndef LOCAL
41+
freopen("huffman.in", "r", stdin), freopen("huffman.out", "w", stdout);
42+
#endif
43+
ios::sync_with_stdio(false), cin.tie(nullptr);
44+
#ifdef LOCAL
45+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
46+
#endif
47+
48+
int _ = 1;
49+
while (_--) solve();
50+
51+
#ifdef LOCAL
52+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
53+
#endif
54+
return 0;
55+
}

Codeforces Gym/100197E.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @file 100197E.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-01-10
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
#define maxn 1005
20+
#define maxm 130
21+
22+
typedef pair<int, int> pii;
23+
24+
int a[maxn], mat[maxm][maxm], f[maxn][maxm];
25+
pii pre[maxn][maxm];
26+
27+
void solve(void) {
28+
int n;
29+
cin >> n;
30+
for (int i = 1; i <= n; i++) cin >> a[i];
31+
int m, s;
32+
cin >> m >> s;
33+
for (int i = 0; i < m; i++)
34+
for (int j = 0; j < s; j++) cin >> mat[i][j];
35+
36+
for (int i = 1; i <= n + 1; i++)
37+
for (int j = 0; j < m; j++) f[i][j] = 1e9;
38+
f[1][0] = 0;
39+
40+
for (int i = 1; i <= n; i++)
41+
for (int j = 0; j < m; j++)
42+
for (int p = 0; p < s; p++)
43+
if (f[i + 1][p & (m - 1)] > f[i][j] + abs(a[i] - mat[j][p]))
44+
f[i + 1][p & (m - 1)] = f[i][j] + abs(a[i] - mat[j][p]), pre[i + 1][p & (m - 1)] = {j, p};
45+
46+
int ansp = 0;
47+
for (int i = 0; i < m; i++)
48+
if (f[n + 1][ansp] > f[n + 1][i]) ansp = i;
49+
50+
cout << f[n + 1][ansp] << endl;
51+
vector<int> ans;
52+
for (int i = n + 1; i > 1; i--) ans.push_back(pre[i][ansp].second), ansp = pre[i][ansp].first;
53+
reverse(ans.begin(), ans.end());
54+
for (auto x : ans) cout << x << ' ';
55+
cout << endl;
56+
57+
return;
58+
}
59+
60+
bool mem2;
61+
62+
int main() {
63+
#ifndef LOCAL
64+
freopen("quant.in", "r", stdin), freopen("quant.out", "w", stdout);
65+
#endif
66+
ios::sync_with_stdio(false), cin.tie(nullptr);
67+
#ifdef LOCAL
68+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
69+
#endif
70+
71+
int _ = 1;
72+
while (_--) solve();
73+
74+
#ifdef LOCAL
75+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
76+
#endif
77+
return 0;
78+
}

0 commit comments

Comments
 (0)