Codeforces Round #321 (Div. 2)-B. Kefa and Company,区间最大值!
2 seconds
256 megabytes
standard input
standard output
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 dunits
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!
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.
Print the maximum total friendship factir that can be reached.
4 5
75 5
0 100
150 20
75 1
100
5 100
0 7
11 32
99 10
46 8
87 54
111
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.
题意很好懂,给定n个人每个人有一定数量的金钱和友谊值,从中选若干人前提是每两个人的金钱差值不能超过给定的d,问所选的人的友谊值最大是多少。
很显然是一个区间最大值问题。我们来看这个前提条件,所选的人中任意两个人的金钱差值要小于d,我们便可以按金钱值从小到大排序,假设从前往后一次增大;这样所选的一些人的金钱最大差值是最后面的那个人的钱减去最前面的那个人的钱,再与d进行比较,如果差值小于d则可以选这些人,大于等于d则要舍弃最前面那个人,当然了,我们可以用第一个人来初始化当前友谊最大值,这样在往后遍历的过程中只需比较所选的连续的几个人(相当于一段区间)的友谊值总和与当前最大友谊值;看代码详解:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100000+10;
struct node
{
int n;
long long v;
}a[N];
long long s[N];
int cmp(node a,node b)
{
return a.n<b.n;//按金钱排序;
}
int main()
{
int m,d,i,j;
while(~scanf("%d%d",&m,&d))
{
memset(s,0,sizeof(s));
for(i=0; i<m; i++)
scanf("%d%I64d",&a[i].n,&a[i].v);
sort(a,a+m,cmp);
s[0]=a[0].v;
j=0;
long long maxx=a[0].v;//初始化当前友谊最大值;
for(i=1; i<m; i++)
{
s[i]=s[i-1]+a[i].v;//将友谊值累加起来,方便计算所选某一段区间的友谊值总和;
maxx=max(maxx,a[i].v);
while(a[i].n-a[j].n>=d)
{
j++;//如果这段区间的金钱最大差大于等于d则舍弃最开始那个,可以自己手推一遍样例就理解了;
}
maxx=max(maxx,s[i]-s[j-1]);
}
printf("%I64d\n",maxx);//注意数据范围用long long;
}
return 0;
}
Codeforces Round #321 (Div. 2)-B. Kefa and Company,区间最大值!的更多相关文章
- 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 (尺取)
排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #321 (Div. 2) C. Kefa and Park dfs
C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps 水题
A. Kefa and First Steps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/58 ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...
- codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)
题目链接:http://www.codeforces.com/problemset/problem/580/A题意:求最长连续非降子序列的长度.C++代码: #include <iostream ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)
http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps【暴力/dp/最长不递减子序列】
A. Kefa and First Steps time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- [USACO 2012 Mar Silver] Landscaping【Edit Distance】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=126 好题啊好题,一开始就输给了这道题的想法! 先把原始状态以及目标状态换 ...
- 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- Errors running builder 'JavaScript Validator'错误处理
MyEclipse2014编辑代码时,只要保存就会报出如下错误信息: Errors occurred during the build. Errors running builder 'JavaScr ...
- 完美单例宏定义(兼容ARC和MRC),项目中可以直接使用
单例模式: 1.永远只分配一块内存来创建对象 2.提供一个类方法, 返回内部唯一的一个对象(一个实例) 3.最好保证init方法也只初始化一次 写一个宏定义文件,传入宏定义函数名,自动生成符合类名的 ...
- 如何安装sql server2005 windows 8
如何安装sql server2005 windows 8 1 从网上下载到本地文件 ,这里使用的是cs_sql_2005_dev_all_dvd 安装版. 2. 点击下图所表示进行安装 3. ...
- Python学习 Day 5 高阶函数 map/reduce filter sorter 返回函数 匿名函数 装饰器 偏函数
高阶函数Higher-orderfunction 变量可以指向函数 >>> abs #abs(-10)是函数调用,而abs是函数本身 <built-in function ab ...
- laravel的socialite微信登录之用户信息
要想获取完整的用户信息如下 { , "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", "nickname" ...
- python常见问题一(安装报错)
常见问题一:我在安装python2.7时,提示错误:'An error occurred during the installation of assembly 'Microsoft.VC90.CRT ...
- mysql创建中文编码库
[2013/5/27 16:06:08] LuoXingchen: Create MySQL Database with Chinese supported: mysql> cre ...
- 00PostgreSQL
PostgreSQL PostgreSQL: The World's Most Advanced Open Source Relational Database;PostgreSQL是一个功能强大的开 ...