A - String Generation
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n >> m;
rep (i, 1, n) cout << (char)(i < m ? 'a' : 'a' + (i - m) % 3); cout << '\n';
}
return 0;
}
B - Find the Spruce
dp, 根据下一层推就行, \(O(n^2)\)
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n >> m; vector<VI> f(n + 2, VI(m + 2));
ll ans = 0;
rep (i, 1, n) cin >> s[i] + 1;
per (i, n, 1) rep (j, 1, m) {
if (s[i][j] == '.') continue;
ans += f[i][j] = min(f[i + 1][j - 1], min(f[i + 1][j], f[i + 1][j + 1])) + 1;
}
cout << ans << '\n';
}
return 0;
}
C - Random Events
贪心
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n >> m;
rep (i ,1, n) cin >> a[i], b[i] = a[i];
sort(b + 1, 1 + n + b);
int c = n;
while (c && a[c] == b[c]) --c;
double ans = c ? 0 : 1;
rep (i, 1, m) {
int r; double p; cin >> r >> p;
if (r >= c) ans += (1 - ans) * p;
}
cout << setiosflags(ios::fixed) << setprecision(6) << ans << '\n';
}
return 0;
}
D - Divide and Summarize
贪心先把能找到的都找出来, \(O(nlog^2n)\)
void solve(int l, int r) {
st.insert(b[r] - b[l - 1]);
int mid = a[l] + a[r] >> 1;
int c = upper_bound(a + l, a + r + 1, mid) - a;
if (c - 1 == r) return;
solve(l, c - 1); solve(c, r);
}
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n >> m; set<ll>().swap(st);
rep (i, 1, n) cin >> a[i];
sort(a + 1, a + 1 + n);
rep (i, 1, n) b[i] = a[i] + b[i - 1];
solve(1, n);
rep (i, 1, m) cin >> k, cout << (st.count(k) ? "YES\n" : "NO\n");
}
return 0;
}
E - Water Level
分类讨论, 发现 x 的数据分为很小, 明显从 x 入手分类讨论, \(O(n)\)
int main() {
IOS; ll k, l, r, t, x, y; cin >> k >> l >> r >> t >> x >> y;
if (x > y) {
if (k + y > r) k -= x, --t;
if ((k - l) / (x - y) >= t) return cout << "Yes\n", 0;
else return cout << "No\n", 0;
} else {
vector<bool> v(x);
while (t > 0) {
if (v[k % x]) return cout << "Yes\n", 0;
v[k % x] = 1;
ll mx = min(t, (k - l) / x);
t -= mx, k -= mx * x;
if (t == 0) return cout << "Yes\n", 0;
--t, k += k + y > r ? -x : y - x;
if (k < l) return cout << "No\n", 0;
}
}
return cout << "Yes\n", 0;
}