「果壳杯」 ROUND 37 (Div. 5) 题解
本帖汇总本场比赛各题的解法与参考程序。
- A. 美丽数字
- B. 营养计划
- C. 编程对决
- D. 数组选数
- E. 飞艇货舱
A. 美丽数字
把三个数从小到大排序为 ,再统计奇数个数。没有奇数时输出 ;三个都是奇数时输出 ;其余情况输出中位数 。
这三个分支恰好对应题目给出的三种审美规则。排序时保留重复数,因此相等数字也不需要特殊处理。数值最大可达 ,用 long long 存储。
每组时间复杂度 ,额外空间复杂度 。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int T; cin >> T;
while (T--) {
long long a[3]; int odd = 0;
for (auto &x : a) { cin >> x; odd += x & 1; }
sort(a, a + 3);
cout << a[odd == 0 ? 2 : odd == 3 ? 0 : 1] << '\n';
}
}
B. 营养计划
每天能够吸收的营养只取决于当天的 ,各天互不影响。按两个分界点依次判断:不超过 时原样输出;不超过 时输出 ;更大时输出 。
整数除法对非负整数会向下取整,正好满足题意。无需使用浮点数。分支按从小到大判断,能够正确包含 和 这两个边界。
总时间复杂度 ,额外空间复杂度 。数值用 long long。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
while (n--) {
long long a; cin >> a;
if (a <= 10000) cout << a << '\n';
else if (a <= 50000) cout << a * 7 / 10 << '\n';
else cout << a / 2 << '\n';
}
}
C. 编程对决
先判断 。题目规定分差恰好等于 时也要重赛,因此这一分支必须包含等号。未触发重赛时,再比较 ,较大者获胜。
三个分支覆盖所有可能:分差在阈值内、分差超出阈值且 、分差超出阈值且 。按规则输出对应字符串即可。输入可达 ,使用 long long。
时间复杂度 ,额外空间复杂度 。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
long long a, b, k; cin >> a >> b >> k;
if (llabs(a - b) <= k) cout << "again\n";
else if (a > b) cout << "karl\n";
else cout << "baozi\n";
}
D. 数组选数
先固定乘数 。如果 ,为了让乘积最小,需要从其余位置中选出两个最小的数;如果 ,乘以负数会反转大小关系,需要选出两个最大的数。
排序后,排除一个位置时,剩余的两个最小值一定在原数组最前面的三个位置中;两个最大值一定在最后面的三个位置中。因此枚举 时,各检查至多三个候选位置,就能取得最优的 ,同时保证三个下标互不相同。
任意三元组都有一个对应的 ,而程序为该 选择了最优的另外两个位置,故枚举所得最小值不会遗漏最优方案。 时任取另外两个位置都会得到 ,也包含在上述处理内。
时间复杂度 ,空间复杂度 。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n; vector<long long> a(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
long long ans = LLONG_MAX;
for (int k = 0; k < n; ++k) {
long long sum = 0; int used = 0;
if (a[k] >= 0) {
for (int i = 0; used < 2; ++i) if (i != k) { sum += a[i]; ++used; }
} else {
for (int i = n - 1; used < 2; --i) if (i != k) { sum += a[i]; ++used; }
}
ans = min(ans, sum * a[k]);
}
cout << ans << '\n';
}
E. 飞艇货舱
一条通道中的合法四联位置只有三种:、、。中间一种是连续四格,左右两种分别跨越一个隔断,且隔断两侧各占两格。
一条通道最多装两组。若左右两组都可用,它们互不重叠,可以装两组;否则,只要三种位置中有一种可用,就能装一组;都不可用则装零组。这个判断枚举了全部合法位置,因而是该通道的最优值。各通道互不影响,总答案等于各通道最优值之和。
可达 ,但只有出现不可用卡槽的通道需要处理。先令答案为 ,用映射为这些通道记录一个十位二进制掩码,再将每条受影响通道的贡献从 改成实际值。
使用有序映射时,时间复杂度 ,空间复杂度 。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
ll n; int m; cin >> n >> m; map<ll, int> blocked;
for (int i = 0; i < m; ++i) {
ll r; int c; cin >> r >> c; blocked[r] |= 1 << (c - 1);
}
int L = 0, C = 0, R = 0;
for (int c = 2; c <= 5; ++c) L |= 1 << (c - 1);
for (int c = 4; c <= 7; ++c) C |= 1 << (c - 1);
for (int c = 6; c <= 9; ++c) R |= 1 << (c - 1);
ll ans = 2 * n;
for (auto item : blocked) {
int mask = item.second;
bool l = !(mask & L), c = !(mask & C), r = !(mask & R);
int best = l && r ? 2 : (l || c || r ? 1 : 0);
ans += best - 2;
}
cout << ans << '\n';
}