【比赛链接】
【题解链接】
【Div.2 A】Mind the Gap
【思路要点】
- 从小到大枚举答案,检查合法性。
- 时间复杂度\(O(Ans*N)\)。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 100005; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } int n, s, a[MAXN]; bool valid(int x) { if (x + s < a[1]) return true; if (x - s > a[n]) return true; for (int i = 1; i < n; i++) if (x - s > a[i] && x + s < a[i + 1]) return true; return false; } int main() { read(n), read(s); for (int i = 1; i <= n; i++) { int x, y; read(x), read(y); a[i] = x * 60 + y; } int ans = 0; while (!valid(ans)) ans++; printf("%d %d\n", ans / 60, ans % 60); return 0; }
【Div.2 B】Watering System
【思路要点】
- 显然我们会优先堵住大小较大的洞,排序后贪心即可。
- 时间复杂度\(O(NLogN)\)。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 100005; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } int main() { int n, A, B, C, T, ans = 0; read(n), read(A), read(B), read(C), read(T); for (int i = 1; i <= n; i++) { int x; read(x); ans += A; if (C >= B) ans += (T - x) * (C - B); } writeln(ans); return 0; }
【Div.2 C/Div.1 A】Stairs and Elevators
【思路要点】
- 不难发现我们只有可能使用起点左起以及右起的第一个楼梯与电梯,总共四种情况。
- 用二分实现即可,时间复杂度\(O(QLogC_l+QLogC_e)\)。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 100005; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } int func(int dist, int v) { return dist / v + (dist % v != 0); } int a[MAXN], b[MAXN]; int main() { int r, n, m, v; read(r), read(r), read(n), read(m), read(v); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 1; i <= m; i++) read(b[i]); int q; read(q); for (int i = 1; i <= q; i++) { int sx, sy, tx, ty; read(sx), read(sy); read(tx), read(ty); if (sx == tx) { writeln(abs(sy - ty)); continue; } int tmp = lower_bound(a + 1, a + n + 1, sy) - a; int tnp = tmp - 1; int ans = 2e9; if (tmp <= n) chkmin(ans, abs(a[tmp] - sy) + abs(a[tmp] - ty) + abs(sx - tx)); if (tnp >= 1) chkmin(ans, abs(a[tnp] - sy) + abs(a[tnp] - ty) + abs(sx - tx)); tmp = lower_bound(b + 1, b + m + 1, sy) - b; tnp = tmp - 1; if (tmp <= m) chkmin(ans, abs(b[tmp]