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 ...
随机推荐
- 洛谷 P3808 【模板】AC自动机(简单版)洛谷 P3796 【模板】AC自动机(加强版)
https://www.cnblogs.com/gtarcoder/p/4820560.html 每个节点的后缀指针fail指针指向: 例如he,she,his,hers的例子(见蓝书P214): 7 ...
- 洛谷 P3285 [SCOI2014]方伯伯的OJ
看到这题,第一眼:平衡树水题,随便做一做好了 然后....我在花了n个小时去调试(维护平衡树父节点)之后,... 调了三个小时后,第一次失败的代码(只能查找排名为k的用户编号,不能根据编号查排名) # ...
- ACM_棋棋棋棋棋(规律题)
棋棋棋棋棋 Time Limit: 2000/1000ms (Java/Others) Problem Description: 在遥远的K次元空间,一年一度的GDUFE-GAME又开始了.每年的GD ...
- 转 PHP Cookies
cookie 常用于识别用户. 什么是 Cookie? cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie. ...
- Ubuntu下编译安装MySQL5.7
tar zxvf mysql-5.7.14.tar.gz cd mysql-5.7.14 第一步: cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \ ...
- ADO.net数据访问
需要引用对应命名空间:System.Data.SqlClient; SqlConnection:连接对象SqlCommand:命令对象SqlDataReader:读取器对象 //造连接字符串 stri ...
- 窗体WINFORM
窗体的事件:删除事件:先将事件页面里面的挂好的事件删除,再删后台代码里面的事件 Panel是一个容器 1.Label -- 文本显示工具Text:显示的文字取值.赋值:lable1.Text 2.Te ...
- MySQL DECIMAL数据类型
https://blog.csdn.net/zyz511919766/article/details/49335565
- skeljs框架关键点使用
global 全局 style.css containers: 1400px;容器宽度 xlarge 超大屏(media: max-width:1680px) style-xlarge.cs ...
- vue 2.0 路由创建的详解过程
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...