diff --git a/Codeforces Gym/100197A.cpp b/Codeforces Gym/100197A.cpp new file mode 100644 index 0000000..c9569d4 --- /dev/null +++ b/Codeforces Gym/100197A.cpp @@ -0,0 +1,110 @@ +/** + * @file 100197A.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +#define maxn 65 +#define maxk 1005 + +typedef vector 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 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; +} \ No newline at end of file diff --git a/Codeforces Gym/100197B.cpp b/Codeforces Gym/100197B.cpp new file mode 100644 index 0000000..3c282fb --- /dev/null +++ b/Codeforces Gym/100197B.cpp @@ -0,0 +1,103 @@ +/** + * @file 100197B.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +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 S[maxn]; +vector 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 pos) { + vector 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 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 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 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; +} \ No newline at end of file diff --git a/Codeforces Gym/100197C.cpp b/Codeforces Gym/100197C.cpp new file mode 100644 index 0000000..dde1d1c --- /dev/null +++ b/Codeforces Gym/100197C.cpp @@ -0,0 +1,55 @@ +/** + * @file 100197C.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +void solve(void) { + int n; + cin >> n; + priority_queue, greater> 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; +} \ No newline at end of file diff --git a/Codeforces Gym/100197E.cpp b/Codeforces Gym/100197E.cpp new file mode 100644 index 0000000..9f2fb81 --- /dev/null +++ b/Codeforces Gym/100197E.cpp @@ -0,0 +1,78 @@ +/** + * @file 100197E.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +#define maxn 1005 +#define maxm 130 + +typedef pair 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 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; +} \ No newline at end of file diff --git a/Codeforces Gym/100197G.cpp b/Codeforces Gym/100197G.cpp new file mode 100644 index 0000000..74f5336 --- /dev/null +++ b/Codeforces Gym/100197G.cpp @@ -0,0 +1,56 @@ +/** + * @file 100197G.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +#define maxn 1005 + +typedef pair pii; + +int a[maxn], b[maxn]; +pii delt[maxn]; + +void solve(void) { + int n, S, T, rest; + cin >> n >> T >> S, rest = T; + for (int i = 1; i <= n; i++) + cin >> a[i], b[i] = a[i] * T / S, rest -= b[i], delt[i] = {((b[i] + 1) * S - a[i] * T) - (a[i] * T - b[i] * S), i}; + sort(delt + 1, delt + n + 1); + for (int i = 1; i <= rest; i++) b[delt[i].second]++; + for (int i = 1; i <= n; i++) cout << b[i] << ' '; + cout << endl; + return; +} + +bool mem2; + +int main() { +#ifndef LOCAL + freopen("input.txt", "r", stdin), freopen("output.txt", "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; +} \ No newline at end of file