A – Nearest Interesting Number
直接模拟。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <bits/stdc++.h> using namespace std; typedef long long LL; template <typename Int> void getInt(Int &x) { x = 0; char ch = getchar(); Int sgn = 1; while (!isdigit(ch)) { if (ch == '-') sgn = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } x *= sgn; } LL n; bool cal(LL x) { LL sum = 0; while (x) { sum += x % 10; x /= 10; } return sum % 4 == 0; } int main() { getInt(n); while (!cal(n)) { ++n; } printf("%lld\n", n); return 0; } |
B – Equalize Prices
区间交集,求可行区间的公共部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <bits/stdc++.h> using namespace std; typedef long long LL; template <typename Int> void getInt(Int &x) { x = 0; char ch = getchar(); Int sgn = 1; while (!isdigit(ch)) { if (ch == '-') sgn = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } x *= sgn; } const LL MAX_N = 100 + 5; LL q, n, k, l[MAX_N], r[MAX_N], a[MAX_N]; LL cal() { getInt(n); getInt(k); for (LL i = 1; i <= n; ++i) { getInt(a[i]); l[i] = max(a[i] - k, 0LL); r[i] = a[i] + k; } LL L = -INT_MAX, R = INT_MAX; for (LL i = 1; i <= n; ++i) { L = max(L, l[i]); R = min(R, r[i]); } if (L <= R) return R; else return -1LL; } int main() { getInt(q); for (LL cs = 1; cs <= q; ++cs) { LL ans = cal(); printf("%lld\n", ans); } return 0; } |
C – Computer Game
转化为在以下约束条件之下求$x$的最大值。
$$ \left \{ \begin{aligned} & k – (ax + by) > 0 \\ & x + y = n \end{aligned} \right . $$
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <bits/stdc++.h> using namespace std; typedef long long LL; template <typename Int> void getInt(Int &x) { x = 0; char ch = getchar(); Int sgn = 1; while (!isdigit(ch)) { if (ch == '-') sgn = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } x *= sgn; } LL q, k, n, a, b; int main() { getInt(q); for (LL cs = 1; cs <= q; ++cs) { getInt(k), getInt(n), getInt(a), getInt(b); LL ans; if (k - b * n <= 0) { puts("-1"); continue; } if ((k - b * n) % (a - b) == 0) { ans = (k - b * n) / (a - b) - 1; } else { ans = (k - b * n) / (a - b); } if (ans >= 0) printf("%lld\n", min(ans, n)); else puts("-1"); } return 0; } |
D – Candy Box (easy version)
对每种糖果的个数排序,从大到小贪心地取。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <bits/stdc++.h> using namespace std; typedef long long LL; template <typename Int> void getInt(Int &x) { x = 0; char ch = getchar(); Int sgn = 1; while (!isdigit(ch)) { if (ch == '-') sgn = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } x *= sgn; } const LL MAX_N = 200000 + 5; LL q, n, a[MAX_N], t[MAX_N], tot; int main() { getInt(q); for (LL cs = 1; cs <= q; ++cs) { getInt(n); for (LL i = 1; i <= n; ++i) getInt(a[i]); map <LL, LL> h; for (LL i = 1; i <= n; ++i) h[a[i]]++; tot = 0; for (auto x: h) t[++tot] = x.second; sort(t + 1, t + tot + 1); // for (LL i = 1; i <= tot; ++i) printf("%lld ", t[i]); puts(""); LL x = t[tot], cnt = t[tot]; for (LL i = tot - 1; i >= 1; --i) { if (t[i] >= x - 1) { --x; cnt += x; } else { x = t[i]; cnt += x; } if (x == 0) break; } printf("%lld\n", cnt); } return 0; } |
说点什么