当前位置:   article > 正文

【第29次CSS CSP】202303 (C++)_csp202303

csp202303

202303-1

 

解题思路:题目说任意两块的交集面积均为 0,所以不需要去重,直接暴力模拟就行了

参考代码O(Kn)

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, a, b;
  4. int g[1005][1005];
  5. int ans;
  6. int main() {
  7. cin >> n >> a >> b;
  8. int x1, y1, x2, y2;
  9. for (int i = 1; i <= n; i++) {
  10. cin >> x1 >> y1 >> x2 >> y2;
  11. int x = min(a, x2) - max(0, x1);
  12. int y = min(b, y2) - max(0, y1);
  13. if (x >= 0 && y >= 0) ans += x * y;
  14. }
  15. cout << ans;
  16. return 0;
  17. }

 

202303-2

 

解题思路:一开始想着用堆一次一个操作,果然超时了

参考代码(70分)O(Klog n)(常数太太大了)

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5 + 5;
  4. int n, m, k;
  5. struct node {
  6. int t, c;
  7. friend bool operator < (node a, node b) {
  8. return a.t < b.t;
  9. }
  10. };
  11. priority_queue<node>q;
  12. int main() {
  13. cin >> n >> m >> k;
  14. int t, c;
  15. for (int i = 1; i <= n; i++) {
  16. cin >> t >> c;
  17. q.push({t, c});
  18. }
  19. while (m - q.top().c >= 0 && q.top().t >= k) {
  20. node now = q.top();
  21. q.pop();
  22. now.t--;
  23. m -= now.c;
  24. q.push(now);
  25. }
  26. cout << q.top().t;
  27. return 0;
  28. }

解题思路.改:先按时间从大到小排序,这样每次操作一个区间,并用前缀和优化,勉强AC

参考代码 最坏O(n^2),但是勉强过了

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5 + 5;
  4. int n, m, k;
  5. struct node {
  6. int t, c;
  7. } a[N];
  8. int s[N];
  9. bool cmp(node a, node b) {
  10. if (a.t == b.t) {
  11. return a.c < b.c;
  12. }
  13. return a.t > b.t;
  14. }
  15. int main() {
  16. cin >> n >> m >> k;
  17. for (int i = 1; i <= n; i++) cin >> a[i].t >> a[i].c;
  18. sort(a + 1, a + n + 1, cmp);
  19. for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i].c;
  20. int cnt, res;
  21. while (m >= 0 && a[1].t > k) {
  22. cnt = n;
  23. for (int i = 2; i <= n; i++) {
  24. if (a[i].t != a[i - 1].t) {
  25. cnt = i - 1;
  26. break;
  27. }
  28. }
  29. res = s[cnt];
  30. if (m - res >= 0) {
  31. m -= res;
  32. for (int i = 1; i <= cnt; i++) a[i].t--;
  33. } else break;
  34. }
  35. cout << a[1].t;
  36. return 0;
  37. }

解题思路.新:发现答案是有连续性的,直接二分答案,快了不少

参考代码 O(nlog n)

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5 + 5;
  4. int n, m, k;
  5. struct node {
  6. int t, c;
  7. } a[N];
  8. bool check(int x) {
  9. int res = 0;
  10. for (int i = 1; i <= n; i++) {
  11. if (a[i].t > x) {
  12. res += a[i].c * (a[i].t - x);
  13. }
  14. }
  15. if (m - res >= 0) return 1;
  16. else return 0;
  17. }
  18. int find() {
  19. int l = k, r = N;
  20. while (l <= r) {
  21. int mid = (l + r) >> 1;
  22. if (check(mid)) r = mid - 1;
  23. else l = mid + 1;
  24. }
  25. return l;
  26. }
  27. int main() {
  28. cin >> n >> m >> k;
  29. for (int i = 1; i <= n; i++) cin >> a[i].t >> a[i].c;
  30. cout << find();
  31. return 0;
  32. }

 

202303-3

解题思路:

参考代码

202303-4

解题思路:

参考代码

202303-5

解题思路:

参考代码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/215749
推荐阅读
相关标签
  

闽ICP备14008679号