比赛链接:Here

1539A. Contest Start

让我们找出哪些参与者会干扰参与者i。这些是数字在 Codeforces Round #727 (Div. 2) - 图1Codeforces Round #727 (Div. 2) - 图2#card=math&code=i%2Bmin%28t%2Fx%EF%BC%8Cn%29)之间的参与者。所以第一个最大值 Codeforces Round #727 (Div. 2) - 图3#card=math&code=%280%EF%BC%8Cn%E2%88%92t%2Fx%29) 参与者将获得 Codeforces Round #727 (Div. 2) - 图4 不满意,下一个参与者将比上一个参与者少获得 Codeforces Round #727 (Div. 2) - 图5 个不满意。所以总的答案是 Codeforces Round #727 (Div. 2) - 图6%E2%8B%85t%2Fx%2Bmin(n%E2%88%921%EF%BC%8Ct%2Fx%E2%88%921)%E2%8B%85min(n%EF%BC%8Ct%2Fx)%2F2#card=math&code=max%280%EF%BC%8Cn%E2%88%92%28t%2Fx%29%E2%8B%85t%2Fx%2Bmin%28n%E2%88%921%EF%BC%8Ct%2Fx%E2%88%921%29%E2%8B%85min%28n%EF%BC%8Ct%2Fx%29%2F2)。

  1. int main() {
  2. cin.tie(nullptr)->sync_with_stdio(false);
  3. int _; for (cin >> _; _--;) {
  4. ll n, x, t;
  5. cin >> n >> x >> t;
  6. ll ans = 0;
  7. if (n > t / x) {
  8. ans = (n - t / x) * (t / x);
  9. t /= x;
  10. ans += t * (t - 1) / 2;
  11. } else ans = n * (n - 1) / 2 ;
  12. cout << ans << "\n";
  13. }
  14. }

1539B. Love Song

伪装成字符串的前缀和


【AC Code】

  1. int main() {
  2. cin.tie(nullptr)->sync_with_stdio(false);
  3. int n, q;
  4. string s;
  5. cin >> n >> q >> s;
  6. vector<int>pre(n + 1);
  7. for (int i = 0; i < n; ++i)
  8. pre[i + 1] = pre[i] + (int)(s[i] - 'a' + 1);
  9. while (q--) {
  10. int l, r;
  11. cin >> l >> r;
  12. cout << pre[r] - pre[l - 1] << "\n";
  13. }
  14. }

1539C. Stable Groups

给定 Codeforces Round #727 (Div. 2) - 图7 个数和 Codeforces Round #727 (Div. 2) - 图8 两个正整数,现有 Codeforces Round #727 (Div. 2) - 图9 个分值为 Codeforces Round #727 (Div. 2) - 图10 的学生,要求排列后相邻两个学生的差的绝对值不大于x,至多能在序列中插入k个任意分值的学生,最少能把原序列加上新的学生后(可不全加或不加)分为几个满足条件的子序列。


一开始觉得可以写反悔贪心(的确也可以),但模拟的过程发现对两个排序以后的相邻数差值再排序的结果即答案。

即按升序补学生

【AC Code】

  1. int main() {
  2. cin.tie(nullptr)->sync_with_stdio(false);
  3. ll n, k, x;
  4. cin >> n >> k >> x;
  5. vector<ll>a(n);
  6. for (ll &x : a)cin >> x;
  7. sort(a.begin(), a.end());
  8. vector<ll>b(n - 1);
  9. for (int i = 0; i < n - 1; ++i)
  10. b[i] = max(0ll, (a[i + 1] - a[i] - 1) / x);
  11. sort(b.begin(), b.end());
  12. ll cnt = n;
  13. for (int i = 0; i < n - 1; ++i) {
  14. if (b[i] <= k) {
  15. k -= b[i];
  16. cnt--;
  17. }
  18. }
  19. cout << cnt << "\n";
  20. }

1539D. PriceFixed

要买 Codeforces Round #727 (Div. 2) - 图11 种商品,每种商品要买 Codeforces Round #727 (Div. 2) - 图12 个,每种商品在购买过 Codeforces Round #727 (Div. 2) - 图13 件任意商品后可以打五折,每件商品原价为2,最少需要多少钱。


很明显单纯为了打折而去买多的商品至少需要1+1元,和直接买没区别,那就是直接贪心了。

【AC Code】

  1. int main() {
  2. cin.tie(nullptr)->sync_with_stdio(false);
  3. int n;
  4. cin >> n;
  5. vector<pair<ll, ll>> v(n);
  6. for (int i = 0; i < n; ++i) {
  7. cin >> v[i].second >> v[i].first;
  8. }
  9. sort(v.begin(), v.end());
  10. int il = 0;
  11. int ir = n - 1;
  12. ll have = 0;
  13. ll ans = 0;
  14. while (il <= ir) {
  15. while (il <= ir && v[il].first <= have) {
  16. ans += v[il].second;
  17. have += v[il].second;
  18. v[il].second = 0;
  19. ++il;
  20. }
  21. while (ir >= il && v[ir].second == 0)
  22. --ir;
  23. if (il > ir) break;
  24. ll cnt = min(v[il].first - have, v[ir].second);
  25. v[ir].second -= cnt;
  26. have += cnt;
  27. ans += cnt * 2;
  28. }
  29. cout << ans << '\n';
  30. }
  1. int main() {
  2. cin.tie(nullptr)->sync_with_stdio(false);
  3. int n; cin >> n;
  4. vector<ll>a(n), b(n);
  5. for (int i = 0; i < n; ++i) {
  6. cin >> a[i] >> b[i];
  7. }
  8. ll total = accumulate(a.begin(), a.end(), 0ll);
  9. ll ans = 2 * total;
  10. vector<ll>order(n);
  11. iota(order.begin(), order.end(), 0); // 生从 0 到 N 的连续整数,即 0、1、2、3、……、N。
  12. sort(order.begin(), order.end(), [&] (int i, int j) {
  13. return b[i] > b[j];
  14. });
  15. ll cnt = total;
  16. for (ll i : order) {
  17. ll tmp = max(0ll, min(cnt - b[i], a[i]));
  18. ans -= tmp;
  19. cnt -= tmp;
  20. }
  21. cout << ans << '\n';
  22. }