维护一个递增的和递减的单调队列

Assignment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 78    Accepted Submission(s): 40

Problem Description
Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a group, the difference of the
ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this.
 
Input
In the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, k (1<=n<=100000, 0<k<=10^9),indicate the company has n persons, k means the maximum difference between abilities of staff in a group is less
than k. The second line contains n integers:a[1],a[2],…,a[n](0<=a[i]<=10^9),indicate the i-th staff’s ability.
 
Output
For each test,output the number of groups.
 
Sample Input
2
4 2
3 1 2 4
10 5
0 3 4 5 2 1 6 7 8 9
 
Sample Output
5
28
Hint
First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]
 
Source
 
Recommend
We have carefully selected several similar problems for you:  5299 5298 

pid=5297" style="color:rgb(26,92,200); text-decoration:none">5297 5296 5295 

 

/* ***********************************************
Author :CKboss
Created Time :2015年07月21日 星期二 12时36分35秒
File Name :1002.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL;
const int maxn=100100; struct Node
{
int val,pos;
}; int n,K;
int a[maxn];
// q1 dizheng q2 dijian
deque<Node> q1,q2; LL ans; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T;
scanf("%d",&T_T);
while(T_T--)
{
ans=0;
while(!q1.empty()) q1.pop_back();
while(!q2.empty()) q2.pop_back(); scanf("%d%d",&n,&K);
for(int i=0;i<n;i++) scanf("%d",a+i); int head=0;
for(int i=0;i<n;i++)
{
Node node = (Node){a[i],i}; /// push q1 tail dizheng
while(!q1.empty())
{
Node b = q1.back();
if(b.val<node.val) q1.pop_back();
else break;
}
q1.push_back(node); /// push q2 tail dijian
while(!q2.empty())
{
Node b = q2.back();
if(b.val>node.val) q2.pop_back();
else break;
}
q2.push_back(node); if(i==0) ans++;
else
{
/// bijiao head
while(true)
{
Node big = q1.front();
Node small = q2.front(); if(big.val-small.val<K) break;
else
{
if(small.pos<big.pos)
{
head=small.pos+1; q2.pop_front();
}
else
{
head=big.pos+1; q1.pop_front();
}
}
}
ans+=i-head+1;
}
} cout<<ans<<endl;
} return 0;
}

HDOJ 5289 Assignment 单调队列的更多相关文章

  1. HDU 5289 Assignment(单调队列)

    题意:给T足数据,然后每组一个n和k,表示n个数,k表示最大同意的能力差,接下来n个数表示n个人的能力,求能力差在k之内的区间有几个 分析:维护一个区间的最大值和最小值,使得他们的差小于k,于是採用单 ...

  2. 二分+RMQ/双端队列/尺取法 HDOJ 5289 Assignment

    题目传送门 /* 题意:问有几个区间最大值-最小值 < k 解法1:枚举左端点,二分右端点,用RMQ(或树状数组)求区间最值,O(nlog(n))复杂度 解法2:用单调队列维护最值,O(n)复杂 ...

  3. HDU - 5289 Assignment (RMQ+二分)(单调队列)

    题目链接: Assignment  题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...

  4. HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...

  5. HDU - 5289:Assignment(单调队列||二分+RMQ||二分+线段树)

    Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this com ...

  6. ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

  7. HDOJ 3415 Max Sum of Max-K-sub-sequence(单调队列)

    因为是circle sequence,可以在序列最后+序列前n项(或前k项);利用前缀和思想,预处理出前i个数的和为sum[i],则i~j的和就为sum[j]-sum[i-1],对于每个j,取最小的s ...

  8. hdu5289(2015多校1)--Assignment(单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  9. HDU 5089 Assignment(rmq+二分 或 单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

随机推荐

  1. 分享开源 Markdown 编辑器 Mditor 的「桌面版」

    简单说明 Mditor 最早只有「组件版」,随着「桌面版」的发布,Mditor 目前有两个版本: 可嵌入到任意 Web 应用的 Embed 版本,这是一桌面版的基础,Repo: https://git ...

  2. WCF 自承载 提供源码

    一.WCF 简单介绍 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,是一套通讯接口.现在比较流行的SOA就可以通过WCF实现. ...

  3. HTML5+JS手机web开发之jQuery Mobile初涉

    一.起始之语 我一直都是在PC上折腾网页的,这会儿怎么风向周边捣鼓起手机网页开发呢?原因是公司原先使用Java开发的产品,耗了不少人力财力,但是最后的效果却不怎么好.因为,Android系统一套东西, ...

  4. 词向量( Distributed Representation)工作原理是什么

    原文:http://www.zhihu.com/question/21714667 4 个回答 83赞同反对,不会显示你的姓名 皮果提 刘鑫.莫教授要养猫.Starling Niohuru 等人赞同 ...

  5. Nubia Z5S官方4.4 UI2.0音频Audio部分简单分析(也适用于其它8974/8064机型)以及降低破音出现几率的方法

    转载请注明出处和网址链接: http://blog.csdn.net/syhost/article/details/31419749 此篇本是在Z5S的官方4.4内測版出来时写的, 主要是看到其在au ...

  6. Python网络爬虫 - 1. 准备工作

    1. 安装Beautiful Soup 下载地址 http://www.crummy.com/software/BeautifulSoup/bs4/download/4.4/ 解压后,进入根目录 控制 ...

  7. GuildBrowser使用AF+MVC 学习笔记

    GuildBrowser 是一个 测试用的项目此为 魔兽世界api的一个展示客户端 项目地址:https://github.com/yehai/GuildBrowser 一:所使用的设计模式:MVC ...

  8. hyper-v虚拟网络配置

    今天重新整整几个月前装的hyper-v,系统早已经装好但是网络不通. hyper-v虚拟网络分为3种:外部网络,内部网络和专用网络. 外部网络:虚拟机可以连接主机和外部网络. 内部网络:虚拟机可以连接 ...

  9. javascript 闭包 通俗解释

    代码段 function foo(){ var a = 2; function bar(){ console.log(a); } return bar; } var baz = foo(); baz( ...

  10. Android 断点续传下载

    断点续传在面试中出现的概率还是比较大的,因为一般的应用都需要. 这个代码是从网上找来的,自己改了点东西,能跑通,但是这个代码并不是最优代码和设计.但是基本思路体现出来了,可以以这个为基础来进行修改.先 ...