Codeforces Round #727 (Div. 2) A~D题题解
比赛链接:Here
1539A. Contest Start
让我们找出哪些参与者会干扰参与者i。这些是数字在
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
ll n, x, t;
cin >> n >> x >> t;
ll ans = 0;
if (n > t / x) {
ans = (n - t / x) * (t / x);
t /= x;
ans += t * (t - 1) / 2;
} else ans = n * (n - 1) / 2 ;
cout << ans << "\n";
}
}
1539B. Love Song
伪装成字符串的前缀和
【AC Code】
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, q;
string s;
cin >> n >> q >> s;
vector<int>pre(n + 1);
for (int i = 0; i < n; ++i)
pre[i + 1] = pre[i] + (int)(s[i] - 'a' + 1);
while (q--) {
int l, r;
cin >> l >> r;
cout << pre[r] - pre[l - 1] << "\n";
}
}
1539C. Stable Groups
给定
一开始觉得可以写反悔贪心(的确也可以),但模拟的过程发现对两个排序以后的相邻数差值再排序的结果即答案。
即按升序补学生
【AC Code】
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
ll n, k, x;
cin >> n >> k >> x;
vector<ll>a(n);
for (ll &x : a)cin >> x;
sort(a.begin(), a.end());
vector<ll>b(n - 1);
for (int i = 0; i < n - 1; ++i)
b[i] = max(0ll, (a[i + 1] - a[i] - 1) / x);
sort(b.begin(), b.end());
ll cnt = n;
for (int i = 0; i < n - 1; ++i) {
if (b[i] <= k) {
k -= b[i];
cnt--;
}
}
cout << cnt << "\n";
}
1539D. PriceFixed
要买
很明显单纯为了打折而去买多的商品至少需要1+1元,和直接买没区别,那就是直接贪心了。
【AC Code】
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<pair<ll, ll>> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i].second >> v[i].first;
}
sort(v.begin(), v.end());
int il = 0;
int ir = n - 1;
ll have = 0;
ll ans = 0;
while (il <= ir) {
while (il <= ir && v[il].first <= have) {
ans += v[il].second;
have += v[il].second;
v[il].second = 0;
++il;
}
while (ir >= il && v[ir].second == 0)
--ir;
if (il > ir) break;
ll cnt = min(v[il].first - have, v[ir].second);
v[ir].second -= cnt;
have += cnt;
ans += cnt * 2;
}
cout << ans << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n; cin >> n;
vector<ll>a(n), b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
}
ll total = accumulate(a.begin(), a.end(), 0ll);
ll ans = 2 * total;
vector<ll>order(n);
iota(order.begin(), order.end(), 0); // 生从 0 到 N 的连续整数,即 0、1、2、3、……、N。
sort(order.begin(), order.end(), [&] (int i, int j) {
return b[i] > b[j];
});
ll cnt = total;
for (ll i : order) {
ll tmp = max(0ll, min(cnt - b[i], a[i]));
ans -= tmp;
cnt -= tmp;
}
cout << ans << '\n';
}
分类:
刷题笔记: CF
【推荐】国内首个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程序员写了一个飞机大战