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

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. Android学习之Http使用Post方式进行数据提交(普通数据和Json数据)

    转自:http://blog.csdn.net/wulianghuan/article/details/8626551 我们知道通过Get方式提交的数据是作为Url地址的一部分进行提交,而且对字节数的 ...

  2. SharePoint 2013 本地创建解决方案

    在之前的博客<SharePoint 2013本地开发解决方案以及远程调试>中,我们介绍了如何通过修改注册表,使SharePoint 2013 解决方案可以本地编辑,也提及了即使修改注册表, ...

  3. CentOS安装mysql*.rpm提示conflicts with file from package的解决的方法

    CentOS 6.5下安装MySql 5.6 解压文件:tar xvf MySQL-5.6.19-1.linux_glibc2.5.x86_64.rpm-bundle.tar 释放出下面文件: MyS ...

  4. Android 下文件cannot execute - Permission denied

    安卓下执行交叉编译的可执行文件发现提示不允许. 原因是mount的方式问题,root后运行 su mount -o rw,remount /mnt/sdcard 就可以了 mount -o rw,re ...

  5. PhD Positions opening at University of Nevada, Reno (Wireless Networking / Cognitive Radio / Wireless Security)

    PhD Positions opening at University of Nevada, RenoDept. of Computer Science and Engineering Researc ...

  6. Linux进程间通信—信号

    三.信号(Signal) 信号是Unix系统中使用的最古老的进程间通信的方法之一.操作系统通过信号来通知某一进程发生了某一种预定好的事件:接收到信号的进程可以选择不同的方式处理该信号,一是可以采用默认 ...

  7. 转载 C++实现的委托机制

    转载 C++实现的委托机制 1.引言 下面的委托实现使用的MyGUI里面的委托实现,MyGUI是一款强大的GUI库,想理解更多的MyGUI信息,猛击这里http://mygui.info/ 最终的代码 ...

  8. 构建高可用Linux服务器一

    1.显示物理CPU个数:cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -1 2.显示每个物理CPU中的core ...

  9. Gnome桌面的下拉式终端: Guake

    什么是Guake? Guake是应用于Gnome环境的下拉式终端.主要由Python编写,使用了一些C,它以GPL2+许可证发布,适用于Linux以及类似的系统.Guake的灵感来源于电脑游戏Quak ...

  10. (转)scala中map与flatMap浅析

    在函数式语言中,函数作为一等公民,可以在任何地方定义,在函数内或函数外,可以作为函数的参数和返回值,可以对函数进行组合.由于命令式编程语言也可以通过类似函数指针的方式来实现高阶函数,函数式的最主要的好 ...