Codeforces 580B: Kefa and Company(前缀和)
http://codeforces.com/problemset/problem/580/B
题意:Kefa有n个朋友,要和这n个朋友中的一些出去,这些朋友有一些钱,并且和Kefa有一定的友谊值,要求这些朋友间的贫富差距(拥有的钱的数量)小于d,要求Kefa邀请出来的朋友的友谊值和最大,并输出这个最大值。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
struct wzy
{
ll friends,money;
}p[maxn];
ll sum[maxn];
//按金钱数升序排序
bool cmp(wzy u,wzy v)
{
return u.money<v.money;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int n,d;
cin>>n>>d;
for(int i=0;i<n;i++)
cin>>p[i].money>>p[i].friends;
sort(p,p+n,cmp);
ms(sum);
// sum数组记录到达第i个朋友的友谊值的总和
sum[0]=p[0].friends;
// 对ans赋初值
ll ans=p[0].friends;
int j=0;
for(int i=1;i<n;i++)
{
// 计算友谊值的前缀和
sum[i]=sum[i-1]+p[i].friends;
ans=max(ans,p[i].friends);
// 直到金钱相差大于等于d的时候,更新ans的值
while(p[i].money-p[j].money>=d)
j++;
ans=max(ans,sum[i]-sum[j-1]);
}
cout<<ans<<endl;
return 0;
}
Codeforces 580B: 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 ...
- 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)
题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...
- 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 ...
- B - Kefa and Company
B - Kefa and Company Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- CodeForces 580B(尺取法)
Kefa and Company 题意:Kefa这个人要去吃饭,他要邀请一些朋友一起去,他的每个朋友有两个属性金钱和关系度,要求邀请的人里边任意两个人之间的金钱差的绝对值不大于d:求被邀请的所有朋友的 ...
- 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) Kefa and Company 二分
原题链接:http://codeforces.com/contest/580/problem/B 题意: 给你一个集合,集合中的每个元素有两个属性,$m_i,s_i$,让你求个子集合,使得集合中的最大 ...
- Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)
排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...
- Codeforces 1132C - Painting the Fence - [前缀和优化]
题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...
随机推荐
- 手把手教你学node.js之使用 superagent 与 cheerio 完成简单爬虫
使用 superagent 与 cheerio 完成简单爬虫 目标 建立一个 lesson 3 项目,在其中编写代码. 当在浏览器中访问 http://localhost:3000/ 时,输出 CNo ...
- devise修改密码
https://ruby-china.org/topics/1314 password/edit不是给你直接改密码用的 这个是忘记密码后,发送重置密码的邮件到你邮箱,同时生成一个token 然后你点那 ...
- NOSQL数据库-Redis
官方提倡使用Linux版的Redis,所以官网值提供了Linux版的Redis下载,我们可以从GitHub上下载window版的Redis,具体链接地址如下: · 官网下载地址:http://redi ...
- source insight 4.0的基本使用方法(转)
源:source insight 4.0的基本使用方法 source insight 4设置
- js 变量、函数提升 与js的预编译有关
参考网址:http://www.codesec.net/view/178491.html 先简单理解下作用域的概念,方便对变量与函数提升的概念的理解 function foo() { var x = ...
- 《Java程序设计》第三章-基础语法
20145221<Java程序设计>第三章-基础语法 总结 教材学习内容总结 类型.变量与运算符 类型 Java可区分为基本类型(Primitive Type)和类类型(Class Typ ...
- stm32 Flash读写[库函数]
stm32 的内置可编程Flash在许多场合具有十分重要的意义.如其支持ICP特性使得开发人员对stm32可以警醒调试开发,可以通过JTAG和SWD接口对stm32进行程序烧写:支持IAP特性使得开发 ...
- Opencv3_Learning
git地址 采用jupyter notebook进行编程,语言为python. 感谢博主--戳 这类东西真的是不是天天打,就是打过就忘.. 有志者事竟成,破釜沉舟,百二秦关终属楚. 苦心人天不负,卧薪 ...
- Hadoop-1.0.4集群搭建笔记
这篇文章介绍的是简单的配置Hadoop集群的方法,适合实验和小型项目用,正式运行的集群需要用更正规的方法和更详细的参数配置,不适合使用这篇文章. 相关随笔: 用python + hadoop stre ...
- python 列表字符串元素乱序
from random import shuffle color = ['] shuffle(color) print(color)