AtCoder Beginner Contest 195 Editorial
AtCoder Beginner Contest 195 Editorial
Problem A - Health M Death(opens new window)
只要检查
- Time complexity is
. - Space complexity is
.
Code(C++)
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int M, H;
cin >> M >> H;
cout << (H % M == 0 ? "Yes\n" : "No\n");
return 0;
}
Problem B - Many Oranges(opens new window)
注意
是以千克为单位,所以需要以 代替
首先先来分析上限:
尽可能使用 A 来达到上限,但问题是可能会有剩余的克数
,我们需要将其分配给
- Time complexity is
). - Space complexity is
.
Code (C++)
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int A, B, W;
cin >> A >> B >> W;
W *= 1000;
int minn = W / B;
int maxn = W / A;
if (minn + (W % B != 0) <= maxn) {
cout << minn + (W % B != 0) << ' ' << maxn << "\n";
} else {
cout << "UNSATISFIABLE\n";
}
return 0;
}
Problem C - Comma(opens new window)
- [1,9]: 9 numbers, each has 0 commas.
- [10,99]: 90 numbers, each has 0 commas.
- [100,999]: 900 numbers, each has 0 commas.
- [1000,9999]: 9000 numbers, each has 1 comma.
基于以上模式可以很简单从
- Time complexity is
. - Space complexity is
.
Code (Rust)
use proconio::input;
fn main() {
input! {
n: usize,
}
let mut base: usize = 1_000;
let mut ans: usize = 0;
let mut cnt = 3;
while base <= n {
let num = (n - base + 1).min(base * 9);
ans += num * (cnt / 3);
cnt += 1;
base *= 10;
}
println!("{}", ans);
}
Code (C++)
using ll = long long;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
ll n, ans = 0;
cin >> n;
if (n > 999) ans += n - 999;
if (n > 999999) ans += n - 999999;
if (n > 999999999) ans += n - 999999999;
if (n > 999999999999) ans += n - 999999999999;
if (n > 999999999999999) ans += n - 999999999999999;
cout << ans << "\n";
return 0;
}
Problem D - Shipping Center(opens new window)
第一眼看过去是线段树问题,但数据范围较小可以暴力找。
对于每个查询,我们收集所有可用的框,并根据其容量以升序对其进行排序。 对于每个盒子,我们从没有使用过的,盒子可以容纳的所有物品中,贪婪地选择最有价值的行李。
- Time complexity is
. - Space complexity is
.
Code (C++)
using ll = long long;
typedef pair pii;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector wv(n);
vector x(m);
for (int i = 0; i < n; ++i) cin >> wv[i].second >> wv[i].first;
for (int i = 0; i < m; ++i) cin >> x[i];
sort(wv.begin(), wv.end(), greater());
while (q--) {
multiset s;
// 避免使用 Set 增加不必要的排序,因为上面已经排好序了
ll ans = 0;
int l, r;
cin >> l >> r;
for (int i = 0; i < l - 1; ++i) s.insert(x[i]);
for (int i = r; i < m; ++i) s.insert(x[i]);
multiset::iterator it;
for (int i = 0; i < n; ++i) {
if ((it = s.lower_bound(wv[i].second)) != s.end())
s.erase(it), ans += wv[i].first;
}
cout << ans << "\n";
// cout << "\n";
}
return 0;
}
Problem E - Lucky 7 Battle(opens new window)
不难发现,在这个游戏中,只有
从后开始,因为我们只知道游戏结束时的赢/输状态:
对于每一步,我们都会枚举所有
如果高桥移动,则他需要
如果Aoki移动,他需要
并且我们只需要首先检查
- Time complexity is
. - Space complexity is
.
using ll = long long;
int p7[8] = {1, 3, 2, 6, 4, 5};
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int l;
cin >> l;
string s, t;
cin >> s >> t;
int mask = 1;
for (int i = l - 1; i >= 0; i--) {
int tg = (p7[(l - 1 - i) % 6] * (s[i] - '0')) % 7;
int nm = (mask << tg);
nm |= (nm >> 7);
nm &= (1 << 7) - 1;
if (t[i] == 'T') mask |= nm;
else
mask &= nm;
// cout << mask << '\n';
}
cout << (mask & 1 ? "Takahashi\n" : "Aoki\n");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 全程不用写代码,我用AI程序员写了一个飞机大战
2020-03-15 LeetCode | 第180场周赛--5356矩阵中的幸运数