Codeforces Round #321 (Div. 2) 580B Kefa and Company(贪心 + 二分)
来源:互联网 发布:免费国际长途电话软件 编辑:程序博客网 时间:2024/05/24 00:53
题目链接:点击打开链接
第一行给出n、d,接下来n行每行两个数,代表一个人的财富程度与友好值,让你从里面选人使得友好值之和最大且任意两人财富值差值小于d。
当时wa了好多发,比赛快结束时一个陌生紫名发来他的代码2333 我爆int以及思路有问题
首先按照钱数从小到大排序n个结构体,b数组存储排序后前x个人的友好总值(x >= 1 && x <= n),而后遍历每个人,二分查找出满足钱
数差小于d的友好值最大的人,用刚刚的b数组即可减去当前人的友好值总和即可得出答案。
AC代码:
#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"using namespace std;const int MAXN = 1e5+5;typedef long long ll;ll n, d, ans, b[MAXN];struct node{/* data */ll mon, fri;}a[MAXN];bool cmp(node a, node b){return a.mon < b.mon;}int main(int argc, char const *argv[]){cin >> n >> d;for(int i = 0; i < n; ++i)cin >> a[i].mon >> a[i].fri;sort(a, a + n, cmp);for(int i = 1; i <= n; ++i)b[i] = a[i - 1].fri + b[i - 1];for(int i = 0; i < n; ++i) {int l = i, r = n;while(r - 1 > l) {int mid = (l + r) >> 1;if(a[mid].mon - a[i].mon < d) l = mid;else r = mid;}ans = max(ans, b[l + 1] - b[i]);}cout << ans << endl;return 0;}
1 0
- Codeforces Round #321 (Div. 2) 580B Kefa and Company(贪心 + 二分)
- Codeforces Round #321 (Div. 2) 580B. Kefa and Company
- Codeforces Round #321 (Div. 2)B. Kefa and Company
- Codeforces Round #321 (Div. 2) B. Kefa and Company
- Codeforces Round #321 (Div. 2)B. Kefa and Company
- 接上一篇 580 B. Kefa and Company [ Codeforces Round #321 (Div. 2)]
- Codeforces Round #321 B Kefa and Company
- codeforces-580B Kefa and Company(二分)
- Codeforces Round #321 (Div. 2)-B. Kefa and Company,区间最大值!
- Codeforces#321 (Div. 2) B. Kefa and Company(前缀和,二分查找)
- CodeForces 580B Kefa and Company <贪心>
- 【CodeForces】580B - Kefa and Company(二分)
- CF#321-B - Kefa and Company-贪心-
- CodeForces 580B Kefa and Company
- Codeforces 580B Kefa and Company
- 【CodeForces】[580B]Kefa and Company
- CodeForces 580B Kefa and Company
- CodeForces 580B Kefa and Company
- POJ 1062 昂贵的聘礼
- Nagios客户端执行oracle检查脚本和远程server端不一致的问题
- 知名APP市场总监教你:如何策划高效的市场推广运营方案(超级干货)
- html5中 data-*属性详解
- Unity3D PoolManage
- Codeforces Round #321 (Div. 2) 580B Kefa and Company(贪心 + 二分)
- solr基本操作代码示例
- 循环添加用户
- 【学习笔记】系列五:linux_nmon监控方法
- iOS7新JavaScriptCore框架入门介绍
- SpannableStringBuilder连续调用setSpan时参数Object what不能使用同一个引用
- openssl加密解密
- iOS开发-------涂鸦板(UIBezierPath 贝赛尔曲线)与 MVC初尝试
- 谷歌卫星地图导出MBTile离线包及应用教程