经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
cf1043D. Mysterious Crime(二分 前缀和)
来源:cnblogs  作者:自为风月马前卒  时间:2018/10/30 9:00:25  对本文有异议

题意

题目链接

Sol

考场上做完前四题的时候大概还剩半个小时吧,那时候已经困的不行了。

看了看E发现好像很可做??

又仔细看了几眼发现这不是sb题么。。。

先考虑两个人,假设贡献分别为\((x, y) (a, b)\)

有两种组合方式,一种是\(x + b\),另一种是\(y + a\)

\(x + b >= y + a\)

那么\(x - y >= b - a\)

因此我们按照\(x - y\)排序,对于每个位置,肯定是某一个前缀全选\(x+b\),除此之外都是\(y+a\)

二分之后前缀和后缀和安排一下

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<map>
  6. #include<vector>
  7. #include<set>
  8. #include<queue>
  9. #include<cmath>
  10. //#include<ext/pb_ds/assoc_container.hpp>
  11. //#include<ext/pb_ds/hash_policy.hpp>
  12. #define Pair pair<int, int>
  13. #define MP(x, y) make_pair(x, y)
  14. #define fi first
  15. #define se second
  16. #define int long long
  17. #define LL long long
  18. #define ull unsigned long long
  19. #define rg register
  20. #define pt(x) printf("%d ", x);
  21. //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
  22. //char buf[(1 << 22)], *p1 = buf, *p2 = buf;
  23. //char obuf[1<<24], *O = obuf;
  24. //void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';}
  25. //#define OS *O++ = ' ';
  26. using namespace std;
  27. //using namespace __gnu_pbds;
  28. const int MAXN = 6e5 + 10, INF = 1e9 + 10, mod = 998244353;
  29. const int base = 137;
  30. const double eps = 1e-9;
  31. inline int read() {
  32. char c = getchar();
  33. int x = 0, f = 1;
  34. while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}
  35. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  36. return x * f;
  37. }
  38. int N, M, x[MAXN], y[MAXN], cha[MAXN];
  39. struct Node {
  40. int x, y, val;
  41. bool operator < (const Node &rhs) const {
  42. return val < rhs.val;
  43. }
  44. }a[MAXN];
  45. int sx[MAXN], sy[MAXN], ans[MAXN];
  46. int calc(int x, int y, int a, int b) {
  47. return min(x + b, y + a);
  48. }
  49. main() {
  50. N = read(); M = read();
  51. for(int i = 1; i <= N; i++) {
  52. x[i] = a[i].x = read(), y[i] = a[i].y = read();
  53. cha[i] = a[i].val = a[i].x - a[i].y;
  54. }
  55. for(int i = 1; i <= M; i++) {
  56. int p = read(), q = read();
  57. int mn = calc(a[p].x, a[p].y, a[q].x, a[q].y);
  58. ans[p] -= mn; ans[q] -= mn;
  59. }
  60. sort(a + 1, a + N + 1);
  61. sort(cha + 1, cha + N + 1);
  62. for(int i = 1; i <= N; i++) sx[i] = sx[i - 1] + a[i].x;
  63. for(int i = N; i >= 1; i--) sy[i] = sy[i + 1] + a[i].y;
  64. for(int i = 1; i <= N; i++) {
  65. int pos = upper_bound(cha + 1, cha + N + 1, x[i] - y[i]) - cha - 1;
  66. ans[i] += y[i] * pos + sx[pos] + (N - pos) * x[i] + sy[pos + 1] - calc(x[i], y[i], x[i], y[i]);
  67. }
  68. for(int i = 1; i <= N; i++) cout << ans[i] << " ";
  69. return 0;
  70. }
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站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号