Skip to content

Commit

Permalink
Codeforces Gym: 2003-2004 Summer Petrozavodsk Camp, Andrew Stankevich…
Browse files Browse the repository at this point in the history
… Contest 2 (ASC 2)
  • Loading branch information
Macesuted committed Jan 11, 2025
1 parent 75ed33a commit 8f1e22d
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 0 deletions.
110 changes: 110 additions & 0 deletions Codeforces Gym/100197A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @file 100197A.cpp
* @author Macesuted ([email protected])
* @date 2025-01-10
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

#define maxn 65
#define maxk 1005

typedef vector<int8_t> bigInt;

int jmp[maxk][30], inq[maxk];
bool tgt[maxk], cont[maxk][30], vis[maxk];
bigInt f[maxn][maxk];

void add(bigInt& a, const bigInt& b) {
if (a.size() < b.size()) a.resize(b.size());
for (int i = 0; i < (int)b.size(); i++) a[i] += b[i];
for (int i = 0; i < (int)a.size(); i++) {
if (a[i] < 10) continue;
if (i + 1 == (int)a.size()) a.push_back(0);
a[i + 1]++, a[i] -= 10;
}
return;
}

void solve(void) {
string sigma;
cin >> sigma;
int m = sigma.size();

int k;
cin >> k;

int S, Tcnt;
cin >> S >> Tcnt;
for (int t = 1, x; t <= Tcnt; t++) cin >> x, tgt[x] = true;

for (int i = 1; i <= k; i++)
for (int j = 1; j <= m; j++) cin >> jmp[i][j];
for (int i = 1; i <= k; i++)
for (int j = 1; j <= m; j++) cin >> cont[i][j];
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= k; i++) vis[i] = !cont[i][j];
for (int i = 1; i <= k; i++) {
if (vis[i]) continue;
stack<int> S;
S.push(i), inq[i]++;
while (!vis[S.top()] && inq[S.top()] == 1) S.push(jmp[S.top()][j]), inq[S.top()]++;
if (!vis[S.top()]) jmp[S.top()][j] = 0;
int ans = jmp[S.top()][j];
while (!S.empty()) inq[S.top()]--, jmp[S.top()][j] = ans, vis[S.top()] = true, S.pop();
}
}

int n;
cin >> n;

f[0][S].push_back(1);

for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
if (f[i - 1][j].empty()) continue;
for (int c = 1; c <= m; c++)
if (jmp[j][c]) add(f[i][jmp[j][c]], f[i - 1][j]);
}

bigInt ans;
ans.push_back(0);
for (int i = 1; i <= k; i++)
if (tgt[i]) add(ans, f[n][i]);

reverse(ans.begin(), ans.end());
for (auto x : ans) cout << (int)x;
cout << endl;

return;
}

bool mem2;

int main() {
#ifndef LOCAL
freopen("dfa.in", "r", stdin), freopen("dfa.out", "w", stdout);
#endif
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
103 changes: 103 additions & 0 deletions Codeforces Gym/100197B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @file 100197B.cpp
* @author Macesuted ([email protected])
* @date 2025-01-10
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

#define maxn 70

int f[maxn][maxn], g[maxn][maxn], pre[maxn][maxn];

stack<int> S[maxn];
vector<string> ans;

void mov(int x, int y) {
int v = S[x].top();
S[x].pop();
ans.push_back("move " + to_string(v) + " from " + to_string(x) + " to " + to_string(y));
if (!S[y].empty()) ans.back() += " atop " + to_string(S[y].top());
S[y].push(v);
return;
}

void solve(int m, int n, int from, int to, vector<int> pos) {
vector<int> x(m + 1);
for (int i = m; i >= 3; i--) x[i] = pre[i][n - 1], n -= x[i];

for (int i = m; i >= 3; i--) {
if (!x[i]) continue;
vector<int> npos = pos;
npos.resize(i - 3), npos.push_back(to);
solve(i, x[i], from, pos[i - 3], npos);
}

mov(from, to);

for (int i = 3; i <= m; i++) {
if (!x[i]) continue;
vector<int> npos = pos;
npos.resize(i - 3), npos.push_back(from);
solve(i, x[i], pos[i - 3], to, npos);
}

return;
}

void solve(void) {
int n, m;
cin >> n >> m;

for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++) f[i][j] = g[i][j] = 1e9;
f[2][1] = 1;

for (int i = 3; i <= m; i++)
for (int j = 1; j <= n; j++) {
f[i][j] = min(int(1e9), 1 + 2 * g[i][j - 1]);
for (int k = j; k <= n; k++)
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;
}

vector<int> pos;
for (int i = 2; i < m; i++) pos.push_back(i);
for (int i = n; i; i--) S[1].push(i);
solve(m, n, 1, m, pos);

cout << ans.size() << endl;
for (auto x : ans) cout << x << endl;

return;
}

bool mem2;

int main() {
#ifndef LOCAL
freopen("hanoi.in", "r", stdin), freopen("hanoi.out", "w", stdout);
#endif

ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
55 changes: 55 additions & 0 deletions Codeforces Gym/100197C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @file 100197C.cpp
* @author Macesuted ([email protected])
* @date 2025-01-10
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

void solve(void) {
int n;
cin >> n;
priority_queue<int64_t, vector<int64_t>, greater<int64_t>> que;
int64_t ans = 0;
for (int i = 1, x; i <= n; i++) cin >> x, que.push(x);
while (que.size() > 1) {
int64_t p = que.top();
que.pop();
int64_t q = que.top();
que.pop();
ans += p + q;
que.push(p + q);
}
cout << ans << endl;
return;
}

bool mem2;

int main() {
#ifndef LOCAL
freopen("huffman.in", "r", stdin), freopen("huffman.out", "w", stdout);
#endif
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
78 changes: 78 additions & 0 deletions Codeforces Gym/100197E.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @file 100197E.cpp
* @author Macesuted ([email protected])
* @date 2025-01-10
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

#define maxn 1005
#define maxm 130

typedef pair<int, int> pii;

int a[maxn], mat[maxm][maxm], f[maxn][maxm];
pii pre[maxn][maxm];

void solve(void) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int m, s;
cin >> m >> s;
for (int i = 0; i < m; i++)
for (int j = 0; j < s; j++) cin >> mat[i][j];

for (int i = 1; i <= n + 1; i++)
for (int j = 0; j < m; j++) f[i][j] = 1e9;
f[1][0] = 0;

for (int i = 1; i <= n; i++)
for (int j = 0; j < m; j++)
for (int p = 0; p < s; p++)
if (f[i + 1][p & (m - 1)] > f[i][j] + abs(a[i] - mat[j][p]))
f[i + 1][p & (m - 1)] = f[i][j] + abs(a[i] - mat[j][p]), pre[i + 1][p & (m - 1)] = {j, p};

int ansp = 0;
for (int i = 0; i < m; i++)
if (f[n + 1][ansp] > f[n + 1][i]) ansp = i;

cout << f[n + 1][ansp] << endl;
vector<int> ans;
for (int i = n + 1; i > 1; i--) ans.push_back(pre[i][ansp].second), ansp = pre[i][ansp].first;
reverse(ans.begin(), ans.end());
for (auto x : ans) cout << x << ' ';
cout << endl;

return;
}

bool mem2;

int main() {
#ifndef LOCAL
freopen("quant.in", "r", stdin), freopen("quant.out", "w", stdout);
#endif
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
Loading

0 comments on commit 8f1e22d

Please sign in to comment.