CSP-J 2023 第二轮认证 题解
[CSP-J 2023] 小苹果
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int cnt = 0, ans = 0; while (n > 0) { cnt++; if (ans == 0 && n % 3 == 1) ans = cnt; n -= (n + 2) / 3; } cout << cnt << " " << ans << "\n"; return 0; }
[CSP-J 2023] 公路
#include <bits/stdc++.h> using namespace std; using LL = long long; const int INF = 0x3f3f3f3f; const int N = 100005; int v[N], a[N]; int main() { int n, d; scanf("%d%d", &n, &d); for (int i = 1; i < n; i++) scanf("%d", &v[i]); int mi = INF; LL ans = 0, s = 0; for (int i = 1; i < n; i++) { //最后一个加油站没读 scanf("%d", &a[i]); s += v[i]; mi = min(mi, a[i]); if (s > 0) { ans += (s + d - 1) / d * mi; s -= (s + d - 1) / d * d; } } printf("%lld\n", ans); return 0; }
[CSP-J 2023] 一元二次方程
#include <bits/stdc++.h> using namespace std; // 输出一个有理数 void print(int fz, int fm) { int g = abs(__gcd(fz, fm)); fz /= g, fm /= g; if (fm == 1) printf("%d", fz); else printf("%d/%d", fz, fm); } int main() { int _; scanf("%d%*d", &_); while (_--) { int a, b, c; scanf("%d%d%d", &a, &b, &c); int d = b * b - 4 * a * c; if (d < 0) printf("NO"); else { int x = 0; for (int i = 1; i * i <= d; i++) { if (d % (i * i) == 0) x = i; } if (x * x == d) { int fz, fm; if (a > 0) fz = -b + x, fm = 2 * a; else fz = b + x, fm = -2 * a; print(fz, fm); } else { int fz, fm; if (a > 0) fz = -b, fm = 2 * a; else fz = b, fm = -2 * a; if (fz != 0) { print(fz, fm); putchar('+'); } int r = d / (x * x); if (a > 0) fz = x, fm = 2 * a; else fz = x, fm = -2 * a; int g = __gcd(fz, fm); fz /= g, fm /= g; if (fm == 1) { if (fz == 1) printf("sqrt(%d)", r); else printf("%d*sqrt(%d)", fz, r); } else if (fz == 1) { printf("sqrt(%d)/%d", r, fm); } else { printf("%d*sqrt(%d)/%d", fz, r, fm); } } } puts(""); } return 0; }
[CSP-J 2023] 旅游巴士
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 10005; vector<pair<int, int>> G[N]; int d[N][105]; int vis[N][105]; struct Node { int u, i, d; bool operator<(const Node &rhs) const { return d > rhs.d; } }; int main() { int n, m, kk; scanf("%d%d%d", &n, &m, &kk); while (m--) { int u, v, w; scanf("%d%d%d", &u, &v, &w); G[u].push_back({v, w}); } memset(d, 0x3f, sizeof d); priority_queue<Node> q; q.push({1, 0, d[1][0] = 0}); while (q.size()) { int u = q.top().u, i = q.top().i; q.pop(); if (vis[u][i]) continue; vis[u][i] = 1; for (auto [v, w] : G[u]) { int t = d[u][i], j = (i + 1) % kk; if (t < w) t += (w - t + kk - 1) / kk * kk; if (d[v][j] > t + 1) q.push({v, j, d[v][j] = t + 1}); } } if (d[n][0] == INF) d[n][0] = -1; printf("%d\n", d[n][0]); return 0; }
CSP-J 2023 第二轮认证 题解
https://blog.mxdyeah.top/mxdyeah_blog_post/26.html