原题链接:http://codeforces.com/contest/580/problem/B

题意:

给你一个集合,集合中的每个元素有两个属性,$m_i,s_i$,让你求个子集合,使得集合中的最大m的差不超过d的情况下,s的和的最大值。

题解:

先排序,然后对于a[i],直接二分a[i].s+d的位置,然后维护一个前缀和,更新答案即可。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#define MAX_N 100005
using namespace std; typedef long long ll; struct node{
public:
ll m,s;
bool operator<(const ll &x)const {
return m < x;
}
}; node a[MAX_N]; bool cmp(node x,node y) {
return x.m < y.m;
} int n;
ll d; ll sum[MAX_N]; int main() {
cin.sync_with_stdio(false);
cin >> n >> d;
for (int i = ; i < n; i++)cin >> a[i].m >> a[i].s;
sort(a, a + n,cmp);
sum[] = a[].s;
for (int i = ; i < n; i++)sum[i] = sum[i - ] + a[i].s;
ll ans = ;
for (int i = ; i < n; i++) {
int t = lower_bound(a, a + n, a[i].m + d) - a - ;
if (i == )ans = max(ans, sum[t]);
else
ans = max(ans, sum[t] - sum[i - ]);
}
cout<<ans<<endl;
return ;
}

Codeforces Round #321 (Div. 2) Kefa and Company 二分的更多相关文章

  1. Codeforces Round #321 (Div. 2) Kefa and Dishes 状压+spfa

    原题链接:http://codeforces.com/contest/580/problem/D 题意: 给你一些一个有向图,求不超过m步的情况下,能获得的最大权值和是多少,点不能重复走. 题解: 令 ...

  2. Codeforces Round #321 (Div. 2) Kefa and First Steps 模拟

    原题连接:http://codeforces.com/contest/580/problem/A 题意: 给你一个序列,问你最长不降子串是多长? 题解: 直接模拟就好了 代码: #include< ...

  3. Codeforces Round #321 (Div. 2) Kefa and Park 深搜

    原题链接: 题意: 给你一棵有根树,某些节点的权值是1,其他的是0,问你从根到叶子节点的权值和不超过m的路径有多少条. 题解: 直接dfs一下就好了. 代码: #include<iostream ...

  4. Codeforces Round #321 (Div. 2) B. Kefa and Company 二分

    B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...

  5. Codeforces Round #321 (Div. 2)-B. Kefa and Company,区间最大值!

    ->链接在此<- B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes ...

  6. Codeforces Round #321 (Div. 2) B 二分+预处理

    B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. Codeforces Round #321 (Div. 2) A, B, C, D, E

    580A. Kefa and First Steps 题目链接: A. Kefa and First Steps 题意描述: 给出一个序列,求最长不降连续子序列多长? 解题思路: 水题,签到 代码: ...

  8. Codeforces Round #321 (Div. 2)

    水 A - Kefa and First Steps /************************************************ * Author :Running_Time ...

  9. 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)

    题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...

随机推荐

  1. PHPExcel探索之旅

    学习地址: https://www.imooc.com/video/8359 下载地址: https://packagist.org/packages/phpoffice/phpexcel 用comp ...

  2. python3 发邮件 smtplib & email 库

    嗨 实现了用163发送到qq的功能,遗留了两个问题: 1. 接收者list会报错:update:因为list[]会传递过去一个真的[]list,改成如下就可以了: before: maillist=[ ...

  3. biological clock--class

    '''this application aimed to cauculate people's biological block about emotional(28), energy(23),int ...

  4. usb driver编写 (转)

    在开头补上LDD3的一句话:如果 USB 驱动没有和另一种处理用户和设备交互的子系统(例如 input, tty, video, 等待)关联, 驱动可使用 USB 主编号为了使用传统的和用户空间之间的 ...

  5. LeetCode(129) Sum Root to Leaf Numbers

    题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...

  6. 学习ucosii要用到的几本书

    转自:http://bbs.elecfans.com/jishu_551275_1_1.html   1.嵌入式实时操作系统μC/OS-II(第2版)  邵贝贝 等译 北京航空航天大学出版社     ...

  7. Linux学习-RPM 软件管理程序: rpm

    RPM 默认安装的路径 一般来说,RPM 类型的文件在安装的时候,会先去读取文件内记载的设定参数内容,然后将该数据用来比对 Linux 系统的环境,以找出是否有属性相依的软件尚未安装的问题. 若环境检 ...

  8. Python虚拟机函数机制之闭包和装饰器(七)

    函数中局部变量的访问 在完成了对函数参数的剖析后,我们再来看看,在Python中,函数的局部变量时如何实现的.前面提到过,函数参数也是一种局部变量.所以,其实局部变量的实现机制与函数参数的实现机制是完 ...

  9. Mybatis(1、核心配置文件、Properties、Settings、typeAliases...)

    Mybatis(1.核心配置文件.Properties.Settings.typeAliases...) 2017年04月23日 22:52:36 阅读数:1527 此章主要介绍sqlMapConfi ...

  10. 栈的push、pop序列 【微软面试100题 第二十九题】

    题目要求: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1.2.3.4.5是某栈的压栈序列,序列4.5.3.2.1是该压栈 ...