B - Kefa and Company
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.
Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!
Input
The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105, ) — the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.
Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively.
Output
Print the maximum total friendship factir that can be reached.
Sample Input
4 5
75 5
0 100
150 20
75 1
100
5 100
0 7
11 32
99 10
46 8
87 54
111
Hint
In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.
In the second sample test we can take all the friends.
题目大意:Kefa要请朋友吃饭,他有n个朋友,这些朋友都两个特征:1.身上所带钱数2.对Kefa的友谊值
如果这些朋友中有人所带钱数比这个朋友所带钱所多与超过d元(包括d),那么这朋友会觉得自己可怜,Kefa
不想让自己的朋友感到可怜,但他又想获得高得友谊值,问Kefa能获得的最高的友谊值是多少
解题:
将朋友所带的钱数和友谊值排序,按钱数从小到大排然后进行判断处理
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std; const int N = ;
typedef long long ll; struct st
{
ll m, s;
}node[N]; int cmp(const void *a, const void *b)
{
st *s1 = (st *)a, *s2 = (st *)b;
if(s1->m != s2->m)
return s1->m - s2->m;
return s1->s - s2->s;
} int main()
{
int n, i;
ll d, sum;
while(~scanf("%d%I64d", &n, &d))
{
for(i = ; i < n ; i++)
scanf("%I64d%I64d", &node[i].m, &node[i].s);
qsort(node, n, sizeof(node[]), cmp);
sum = i = ;
int j = ;
ll Max = ;
while(i < n)
{
if(node[i].m - node[j].m >= d)
{
sum -= node[j].s;
j++;
}
else
{
sum += node[i].s;
i++;
}
Max = max(Max, sum);
}
printf("%I64d\n", Max);
}
return ;
}
B - Kefa and Company的更多相关文章
- CF 321B Kefa and Company(贪心)
题目链接: 传送门 Kefa and Company time limit per test:2 second memory limit per test:256 megabytes Desc ...
- 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 ...
- 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 ...
- CF580B Kefa and Company 尺取法
Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa ...
- Codeforces 580B: Kefa and Company(前缀和)
http://codeforces.com/problemset/problem/580/B 题意:Kefa有n个朋友,要和这n个朋友中的一些出去,这些朋友有一些钱,并且和Kefa有一定的友谊值,要求 ...
- [CF580B]Kefa and Company(滑动窗口)
题目链接:http://codeforces.com/problemset/problem/580/B 某人有n个朋友,这n个朋友有钱数m和关系s两个属性.问如何选择朋友,使得这些朋友之间s最大差距小 ...
- 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)
题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...
- Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)
排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...
- Codeforces Round #321 (Div. 2) Kefa and Company 二分
原题链接:http://codeforces.com/contest/580/problem/B 题意: 给你一个集合,集合中的每个元素有两个属性,$m_i,s_i$,让你求个子集合,使得集合中的最大 ...
随机推荐
- UVA 11354 Bond 邦德 (RMQ,最小瓶颈MST)
题意: n个城市,m条路,每条路有个危险值,要使得从s走到t的危险值最小.回答q个询问,每个询问有s和t,要求输出从s到t最小的危险值.(5万个点,10万条边) 思路: 其实要求的是任意点对之间的最小 ...
- 20160202.CCPP体系详解(0012天)
内容概要:C语言控制语句题库.doc 第三章 控制语句 一.选择题 1. 以下语句中无限循环语句是[B]. A)for(;2&5;); B)while(1,2,3); -> while( ...
- Linux初识(转)
文件系统是linux的一个十分基础的知识,同时也是学习linux的必备知识. 本文将站在一个较高的视图来了解linux的文件系统,主要包括了linux磁盘分区和目录.挂载基本原理.文件存储结构.软链接 ...
- poj 2661 Factstone Benchmark (Stirling数)
//题意是对于给定的x,求满足n! <= 2^(2^x)的最大的n//两边同取以二为底的对数,可得: lg2(n!) <= 2^x 1. log2(n!) = log2(1) + lo ...
- WCF tcpTrace.exe配置
Web.config <?xml version="1.0" encoding="utf-8"?> <configuration> &l ...
- Hadoop1.1.2伪分布式安装笔记
一.设置Linux的静态IP 修改桌面图标修改,或者修改配置文件修改 1.先执行ifconfig,得到网络设备的名称eth0 2.编辑/etc/sysconfig/network-scripts/if ...
- markdown插件学习
1.Markdown的定义 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式.它用简洁的语法代替排版,而不像一般我们用的字处理软件Wo ...
- PHP QR CODE生成二维码
用法: <?php include "./phpqrcode/phpqrcode.php"; $value="http://www.xxx.com"; $ ...
- 设置TextView控件的背景透明度和字体透明度
TextView tv = (TextView) findViewById(R.id.xx); 第1种:tv.setBackgroundColor(Color.argb(255, 0, 255, 0) ...
- Delphi 提示在Delphi的IDE中,按Ctrl+Shift+G键可以为一个接口生成一个新的GUID。
对于Object Pascal语言来说,最近一段时间最有意义的改进就是从Delphi3开始支持接口(interface),接口定义了能够与一个对象进行交互操作的一组过程和函数.对一个接口进行定义包含两 ...