经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
洛谷P4165 [SCOI2007]组队(排序 堆)
来源:cnblogs  作者:自为风月马前卒  时间:2018/12/3 16:08:59  对本文有异议

题意

题目链接

Sol

跟我一起大喊:n方过百万,暴力踩标算!

一个很显然的思路是枚举\(H, S\)的最小值算,复杂度\(O(n^3)\)

我们可以把式子整理一下,变成

\[A H_i + B S_i \leqslant C + AminH + BminS\]

首先按\(H\)排序

考虑去从大到小枚举\(AminH\),同时用个vector \(n^2\)维护\(S\)序列(直接\(lowerbound + insert\))

再从大到小枚举\(BminS\),同时用堆维护\(AH_i + B_i\),当堆顶不满足条件的时候直接弹掉即可,用堆内元素更新答案

没错在BZOJ上被卡了

  1. #include<bits/stdc++.h>
  2. #include<ext/pb_ds/assoc_container.hpp>
  3. #include<ext/pb_ds/hash_policy.hpp>
  4. #include<ext/pb_ds/priority_queue.hpp>
  5. #define Pair pair<int, int>
  6. #define MP make_pair
  7. #define fi first
  8. #define se second
  9. using namespace std;
  10. const int MAXN = 5001, mod = 1e9 + 7;
  11. inline int read() {
  12. char c = getchar(); int x = 0, f = 1;
  13. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  14. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  15. return x * f;
  16. }
  17. int N, A, B, C, th[MAXN], ts[MAXN];
  18. struct Node {
  19. int h, s, id;
  20. bool operator < (const Node &rhs) const {
  21. return h < rhs.h;
  22. }
  23. }a[MAXN];
  24. signed main() {
  25. N = read(); A = read(); B = read(); C = read();
  26. for(int i = 1; i <= N; i++) th[i] = a[i].h = read(), a[i].s = read();
  27. sort(th + 1, th + N + 1);
  28. sort(a + 1, a + N + 1);
  29. int ans = 0;
  30. vector<Pair> v;
  31. for(int i = N; i >= 1; i--) {
  32. Pair now = MP(B * a[i].s, A * a[i].h + B * a[i].s);
  33. v.insert(lower_bound(v.begin(), v.end(), now), now);
  34. //__gnu_pbds::priority_queue<int> q;
  35. priority_queue<int> q;
  36. int r = v.size() - 1;
  37. for(int j = r; j >= 0; j--) {
  38. q.push(v[j].se);
  39. while(!q.empty() && q.top() > C + A * th[i] + v[j].fi) q.pop();
  40. ans = max(ans, (int)q.size());
  41. }
  42. }
  43. cout << ans;
  44. return 0;
  45. }
  46. /*
  47. */
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号