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 ...
随机推荐
- python16_day36【爬虫1】
一.requests 1. GET请求 # 1.无参数实例 import requests ret = requests.get('https://github.com/timeline.json') ...
- SecureCRT 会话空闲时超时退出处理
参考文章:http://www.cnblogs.com/xuxm2007/archive/2011/04/21/2023611.html http://yunwei.blog.51cto.com/38 ...
- QT之QML控件篇
QT quick中提供了很多的实用控件widget,下面介绍几种常用的. 这部分介绍基本是参照QtCretator提供的帮助文档,一定要学会使用,不明白的去查找帮助. Item 基本上所有的可是控件的 ...
- 【转载】package-info
本文是转载,原文地址:http://strong-life-126-com.iteye.com/blog/806246 package-info.java对于经常使用外部包的程序员来说应该是熟悉陌生人 ...
- c++第二十四天
p126~p128: 1.关系运算符作用于算数类型和指针类型. 2.逻辑运算符作用于任意能转换成布尔类型值的类型. 3.以上两种运算的运算对象和运算结果都是右值. 4.逻辑与和逻辑或的运算策略:短路求 ...
- AOP 增强方法
Spring AOP 提供了 5 种类型的通知,它们分别是 Before Advice(前置通知).After Returning Advice(后置通知).Interception Around A ...
- 使用javascript模拟常见数据结构(一)
数据结构和算法可算是每个程序员的必备技能,而随着前端工作的深入,对于数据结构的知识真的是越来越需要掌握了.好了,于是乎最近看了<javascript数据结构和算法>,算是对于后面的使用C语 ...
- C#中标准Dispose模式的实现(转载)
需要明确一下C#程序(或者说.NET)中的资源.简单的说来,C#中的每一个类型都代表一种资源,而资源又分为两类: 托管资源:由CLR管理分配和释放的资源,即由CLR里new出来的对象: 非托管资源:w ...
- HTML之页面镶嵌体验
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- oracle会自动收集统计信息-记住哦
oracle自动收集统计信息,周一至周五 时间:22:00:00 oracle自动收集统计信息,周六.周日 时间:06:00:00