题目链接:http://codeforces.com/problemset/problem/580/B

某人有n个朋友,这n个朋友有钱数m和关系s两个属性。问如何选择朋友,使得这些朋友之间s最大差距小于d并且钱数是最多。

可以用滑动窗口,将m从小到大,s从大到小排列,这时在一个队列里维护队首和队尾,假如队首和队尾的s差距≥d时,就把队尾扔掉队首入队否则就仅队首入队。此时更新一下当前最大值。

 #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm> using namespace std; typedef long long LL;
typedef struct Node {
int m, s;
}Node;
const int maxn = ;
int n, d;
Node f[maxn]; bool cmp(Node a, Node b) {
if(a.m == b.m) return a.s > b.s;
return a.m < b.m;
} int main() {
// freopen("in", "r", stdin);
scanf("%d%d",&n,&d);
for(int i = ; i <= n; i++) {
scanf("%d%d",&f[i].m,&f[i].s);
}
sort(f+, f+n+, cmp);
int r = ;
int cnt = ;
LL ret = , cur = ;
for(int l = ; l <= n; l++) {
while(r <= n && f[r].m - f[l].m < d) {
cur += f[r].s;
r++;
cnt++;
}
ret = max(cur, ret);
cur -= f[l].s;
}
cout << ret << endl;
return ;
}

[CF580B]Kefa and Company(滑动窗口)的更多相关文章

  1. CF580B Kefa and Company 尺取法

    Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa ...

  2. [LeetCode] Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  3. TCP/IP 协议中的滑动窗口

    一个例子明白发送缓冲区.接受缓冲区.滑动窗口协议之间的关系. 在上面的几篇文章中简单介绍了上述几个概念在TCP网络编程中的关系,也对应了几个基本socket系统调用的几个行为,这里再列举一个例子,由于 ...

  4. Storm Windowing storm滑动窗口简介

    Storm Windowing 简介 Storm可同时处理窗口内的所有tuple.窗口可以从时间或数量上来划分,由如下两个因素决定: 窗口的长度,可以是时间间隔或Tuple数量: 滑动间隔(slidi ...

  5. lintcode 滑动窗口的最大值(双端队列)

    题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为  ...

  6. TCP 三次握手四次挥手, ack 报文的大小.tcp和udp的不同之处、tcp如何保证可靠的、tcp滑动窗口解释

    一.TCP三次握手和四次挥手,ACK报文的大小 首先连接需要三次握手,释放连接需要四次挥手 然后看一下连接的具体请求: [注意]中断连接端可以是Client端,也可以是Server端. [注意] 在T ...

  7. tcp协议头窗口,滑动窗口,流控制,拥塞控制关系

    参考文章 TCP 的那些事儿(下) http://coolshell.cn/articles/11609.html tcp/ip详解--拥塞控制 & 慢启动 快恢复 拥塞避免 http://b ...

  8. CF 321B Kefa and Company(贪心)

    题目链接: 传送门 Kefa and Company time limit per test:2 second     memory limit per test:256 megabytes Desc ...

  9. CodeForces 701C They Are Everywhere (滑动窗口)

    题目链接:http://codeforces.com/problemset/problem/701/C 题意:找到字符串中能包含所有元素的最短字符串长度. 利用“滑动窗口”解题 解题思路: 1. 遍历 ...

随机推荐

  1. 【BZOJ】【2753】【SCOI2012】滑雪与时间胶囊

    Kruskal/最小树形图 然而蒟蒻并不会做这题>_> 本来以为是有向图最小生成树,即最小树形图,但这数据范围有点…… 膜拜了zyf的题解:http://www.cnblogs.com/z ...

  2. javascript刷新页面的方法

    Javascript刷新页面的几种方法: 1 history.go(0) 2 location.reload() 3 location=location 4 location.assign(locat ...

  3. nodeJs 初探 ~

    今天晚上,开始时间了一下nodejs,跟着 Node入门 一步步的往下走.对node开发也有了初步的了解. 期间没有碰到什么问题,只有在最后的时候,碰到了几个问题.在这里记录一下: 1 . cross ...

  4. Notes on the Dirichlet Distribution and Dirichlet Process

    Notes on the Dirichlet Distribution and Dirichlet Process In [3]: %matplotlib inline   Note: I wrote ...

  5. js获取,设置FCKeditor内容

    // 获取编辑器中HTML内容 function getEditorHTMLContents(EditorName) {     var oEditor = FCKeditorAPI.GetInsta ...

  6. 关于mysql 连接数

    mysql基本维护,就必须要知道连接数 进入mysql ,show status    Threads_connected  当前的连接数    Connections  试图连接到(不管是否成功)M ...

  7. Codeforces Round #254DIV2

    C:万恶的一道题: 开始想用贪心做:每次去除一个点,相应的去除相连边,结果BUG了,想想也对,因为有一次去掉多个点的情况, 后来被送一助攻,队友给出一个猜想:可能枚举一条边即可产生最大值:然后小小证明 ...

  8. javascript设计模式--单例模式(Singleton)

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  9. eclipse运行WordCount

    1) 可以完全参考http://www.cnblogs.com/archimedes/p/4539751.html在eclipse下创建MapReduce工程,创建了MR工程,并完成WordCount ...

  10. JSP 页面打印

    <HTML><HEAD><TITLE>javascript打印-打印页面设置-打印预览代码</TITLE> <META http-equiv=Co ...