【比赛链接】
【题解链接】
**【A】**Game Shopping
【思路要点】
- 按照题意模拟。
- 时间复杂度 $ O(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 a[MAXN], b[MAXN]; int main() { int n, m; read(n), read(m); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 1; i <= m; i++) read(b[i]); int ans = 0, pos = 1; for (int i = 1; i <= n && pos <= m; i++) if (b[pos] >= a[i]) { pos++; ans++; } writeln(ans); return 0; }
**【B】**Minimum Ternary String
【思路要点】
- 题目中 $ 0 $ 和 $ 2 $ 的相对位置始终不会发生改变,可以看做 $ 1 $ 可以自由移动, $ 0 $ 和 $ 2 $ 留在原地。
- 将所有 $ 1 $ 插入到第一个 2 2 2 之前即可,若没有出现 2 2 2 ,则插到字符串末尾。
- 时间复杂度 $ O(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(""); } char s[MAXN]; int main() { scanf("%s", s + 1); int n = strlen(s + 1), cnt = 0; for (int i = 1; i <= n; i++) if (s[i] == '1') cnt++; for (int i = 1; i <= n; i++) if (s[i] != '1') { if (s[i] == '0') putchar(s[i]); else { if (cnt > 0) while (cnt--) putchar('1'); putchar(s[i]); } } if (cnt > 0) while (cnt--) putchar('1'); return 0; }
**【C】**Annoying Present
【思路要点】
- 使平均数最大即为使总和最大,若 d i ≥ 0 d_i≥0 di≥0 ,则选择在 1 1 1 号位置操作,否则在中间位置操作即可。
- 中间量可能很大,需要 l o n g d o u b l e long\ double long double。
- 时间复杂度 $ O(M) $ 。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 100005; template <typename T> void chkmax(T &x