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 ...
随机推荐
- SpringCloud开发学习总结(五)—— 服务容错保护Hystrix
在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式相互依赖.但由于每个单元都在不同的进程中运行,一来通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身 ...
- 在Paint事件中绘制控件(边框)
单纯的自己记录,将来会继续添加,侥幸被大家发现了的话请不要太鄙视... private void panel4_Paint(object sender, PaintEventArgs e) { Con ...
- Java 线程实例 刷碗烧水和倒计时
线程——烧水刷碗和倒计时实例 (一)烧水刷碗 刷碗的同时烧水:下面是碗的程序: 下面是烧水的程序:在水的实现类中,调用了Thread线程,让烧水刷碗同时进行. 注意:刷碗2s一次,烧水10s (二)1 ...
- Swift 中的基础语法(二)
1.Swift 中的函数 /// 函数的定义 /// /// - Parameters: /// - x: 形参 /// - y: 形参 /// - Returns: 返回值 func sum(x: ...
- [BZOJ1085][SCOI2005]骑士精神 搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1085 大的思路是迭代加深搜索,我们加一个明显的剪枝,当棋盘中位置不对的骑士的数目加上已经走 ...
- vue-router之 beforeRouteEnter
beforeRouteEnter在每次路由切换都执行 ,而项目优化后,切换路由mounted只在最开始执行一次 beforeRouteEnter的具体用法可参考官方文档 https://cn.vuej ...
- Android基础TOP3:线性布局的特点,常用属性,及权重值
线性布局是一种让视图水平或者垂直布排列的布局: 常用属性: androuid:orientation :表示布局方向 取值vertical表示垂直布局 取值horizontal表示水平布局 andro ...
- [Android]如何实现无限滚动的ListViw/GridView(翻译)
ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们提供了一个良好,整洁的用户体验 ...
- [转] NTFS Permission issue with TAKEOWN & ICACLS
(转自:NTFS Permission issue with TAKEOWN & ICACLS - SAUGATA 原文日期:2013.11.19) Most of us using TA ...
- flask的基本搭建
from flask import Flask app = Flask(__name__) @app.route("/")def index(): return "ok& ...