「题解」Codeforces Round 894 (Div. 3)
A. Gift Carpet
Problem
Sol & Code
签到题
#include <bits/stdc++.h>
#define N 21
typedef long long ll;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int T, n, m, a[4];
std::string s[N];
int main() {
scanf("%d", &T);
a[0] = 'v', a[1] = 'i', a[2] = 'k', a[3] = 'a';
while (T--) {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) std::cin >> s[i];
int cnt = 0, okay = 0;
for (int j = 0; j < m; ++j) {
for (int i = 1; i <= n; ++i) {
if (s[i][j] == a[cnt]) {
++cnt;
break;
}
}
if (cnt == 4) { okay = 1; break; }
}
puts(okay ? "YES" : "NO");
}
return 0;
}
B. Sequence Game
Problem
Sol & Code
升序直接接到答案序列后,降序先加个一在答案序列后再加入答案序列。
#include <bits/stdc++.h>
#define N 200001
typedef long long ll;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int T, n, ans[N << 1];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
int last, cnt = 0;
scanf("%d", &last);
ans[++cnt] = last;
for (int i = 2, x; i <= n; ++i) {
scanf("%d", &x);
if (x >= last) { ans[++cnt] = x; last = x; }
else { ans[++cnt] = 1, ans[++cnt] = x; last = x; }
}
printf("%d\n", cnt);
for (int i = 1; i <= cnt; ++i) {
printf("%d ", ans[i]);
}
puts("");
}
return 0;
}
C. Flower City Fence
Problem
Sol & Code
长度为 \(n\) 的木板会对 \(1\sim n\) 有 \(1\) 的贡献,可以差分记录贡献最后看两种摆放时候一致。
#include <bits/stdc++.h>
#define N 200518
typedef long long ll;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int T, n, a[N], ans[N];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) ans[i] = 0, scanf("%d", &a[i]);
std::sort(a + 1, a + n + 1);
if (a[n] != n) { puts("NO"); continue; }
for (int i = 1; i <= n; ++i) {
++ans[1], --ans[a[i] + 1];
}
bool okay = true;
for (int i = 1; i <= n; ++i) {
ans[i] += ans[i - 1];
if (ans[i] != a[n - i + 1]) { okay = false; break; }
}
if (okay) puts("YES");
else puts("NO");
}
return 0;
}
D. Ice Cream Balls
Problem
Sol & Code
\(n\) 种不同的球有 \(\dfrac{n(n-1)}{2}\) 种不同的组合,相同种类的球有 \(1\) 种组合,计算出不超出题目要求的最大的 \(n\) 最后加上剩余数量即可。
#include <bits/stdc++.h>
typedef long long ll;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
ll n;
int T;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%lld", &n);
ll k = sqrt(2ll * n);
while (k * (k - 1) <= 2ll * n) ++k;
while (k * (k - 1) > 2ll * n) --k;
printf("%lld\n", k + (n - k * (k - 1) / 2));
}
return 0;
}
E. Kolya and Movie Theatre
Problem
Sol & Code
假设我们在 \(a,b,c\) 三天去看电影,那么 \(d\) 的贡献为 \(-(a-0)d-(b-a)d-(c-b)d = -cd\) 可见 \(d\) 的贡献只与最后是在哪天看的电影有关。
枚举这个最后一天 \(x\),想要知道最后一天为 \(x\) 的答案还要统计前 \(x - 1\) 个数中最多选 \(m - 1\) 个数的和的最大值,优先队列可做。
#include <bits/stdc++.h>
typedef long long ll;
ll max(ll a, ll b) { return a > b ? a : b; }
int T, n, m, d;
std::priority_queue<int> q;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d %d %d", &n, &m, &d);
ll ans = 0, res = 0;
for (int i = 1, x; i <= n; ++i) {
scanf("%d", &x);
if (x > 0) {
if (q.size() < m) {
q.push(-x);
res += x;
} else {
if (-q.top() < x) {
res += q.top();
q.pop();
res += x;
q.push(-x);
}
}
ans = max(ans, res - 1ll * i * d);
}
}
printf("%lld\n", ans);
while (!q.empty()) q.pop();
}
return 0;
}
F. Magic Will Save the World
Problem
Sol & Code
发现时间越久成功概率越大即有单调性,二分答案。
问题来到了如何判断是否可行。
问题可以转化为有两个背包,容量分别为 \(a,b\) 能否将物品全部装下,因此每个背包要尽量装,计算一次 \(0-1\) 背包看剩下的物品能否用另一个背包装下即可。
复杂度好像有点悬,加个小优化。
#include <bits/stdc++.h>
#define N 101
typedef long long ll;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int g[1000518];
int T, n, w, f, s[N], pre[N];
bool check(int k) {
ll a = 1ll * k * w, b = 1ll * k * f;
for (int i = 0; i <= n; ++i) {
if (pre[i] <= a && pre[n] - pre[i] <= b) return true;
if (pre[i] > a) break;
}
for (int i = a; i >= 0; --i) g[i] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = a; j >= s[i]; --j) {
g[j] = max(g[j], g[j - s[i]] + s[i]);
}
}
return (pre[n] - g[a] <= b);
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d %d", &w, &f);
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &s[i]);
pre[i] = pre[i - 1] + s[i];
}
int l = 1, r = 1000000;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) r = mid - 1;
else l = mid + 1;
}
printf("%d\n", r + 1);
}
return 0;
}