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.

InputIn 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.OutputFor 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

题意:求有多少个区间,其最大值和最小值的差小于K。

思路:从左到右,一次求以i为右边界的区间个数,这样的话,左边界一定是不移动,或者右移的。所以我们可以用单调队列,用两个队列表示单增和单减即可维护。    或者用二分得到,每次RMQ验证区间差行否。

(虽然做过,但是我一眼还是想到的是分治,而不是简单方法。。。罪过。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int a[maxn],h1,t1,q1[maxn],h2,t2,q2[maxn],pos; long long ans;
int main()
{
int T,N,K;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&K);
rep(i,,N) scanf("%d",&a[i]);
h1=h2=; t1=t2=; ans=; pos=;
rep(i,,N){
while(h1>=t1&&a[q1[h1]]<a[i]) h1--;
while(h2>=t2&&a[q2[h2]]>a[i]) h2--;
q1[++h1]=i; q2[++h2]=i;
while(t1<=h1&&t2<=h2&&a[q1[t1]]-a[q2[t2]]>=K){
pos++;
while(q1[t1]<=pos) t1++;
while(q2[t2]<=pos) t2++;
}
ans+=i-pos;
}
printf("%lld\n",ans);
}
return ;
}

HDU - 5289:Assignment(单调队列||二分+RMQ||二分+线段树)的更多相关文章

  1. HDU 5289 Assignment(单调队列)

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

  2. HDOJ 5289 Assignment 单调队列

    维护一个递增的和递减的单调队列 Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  3. BZOJ.1758.[WC2010]重建计划(分数规划 点分治 单调队列/长链剖分 线段树)

    题目链接 BZOJ 洛谷 点分治 单调队列: 二分答案,然后判断是否存在一条长度在\([L,R]\)的路径满足权值和非负.可以点分治. 对于(距当前根节点)深度为\(d\)的一条路径,可以用其它子树深 ...

  4. HDU 5289 Assignment [优先队列 贪心]

    HDU 5289 - Assignment http://acm.hdu.edu.cn/showproblem.php?pid=5289 Tom owns a company and he is th ...

  5. HDU 5289 Assignment

    题意:求一段长度为n的序列里有多少个子区间内的最大值减最小值小于k. 解法:RMQ+尺取法或单调队列.RMQ可以用st或者线段树,尺取法以前貌似YY出来过……只是不知道是这个东西…… 设两个标记l和r ...

  6. hdu 5266 pog loves szh III(lca + 线段树)

    I - pog loves szh III Time Limit:6000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I ...

  7. HDU 2795 Billboard(宣传栏贴公告,线段树应用)

    HDU 2795 Billboard(宣传栏贴公告,线段树应用) ACM 题目地址:HDU 2795 Billboard 题意:  要在h*w宣传栏上贴公告,每条公告的高度都是为1的,并且每条公告都要 ...

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

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

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

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

随机推荐

  1. 使用Python实现基于图像识别的iOS自动化测试

    相对于Android来说,iOS比较封闭.这一点,在设计和评估自动化测试方案的时候感觉尤其强烈.iOS平台上没有特别好用的自动化测试工具.苹果针对iOS提供了UI Automation的Instrum ...

  2. Winform与WPF异步修改控件属性

    Winform方式:     if (this.InvokeRequired)             {                 this.Invoke(                   ...

  3. 快速将对象转化为JSON格式

    1.导入阿里巴巴fastjson包. <!-- fastJson将对象转化为Json对象 --> <dependency> <groupId>com.alibaba ...

  4. 你知道uwsgi???

    第一步,打开https://www.google.com.hk 第二步,输入how to restart uwsgi

  5. SQL优化之列裁剪和投影消除

    列裁剪 对于没用到的列,则没有必要读取它们的数据去浪费无谓的IO 比如我们有一张表table1,它含有四列数据(a,b,c,d).当我们执行查询select a from table1 where c ...

  6. React菜鸟食谱

    JSX 用小括号包裹代码防止分号自动插入的bug,用大括号包裹里面的表达式 切记你使用了大括号包裹的 JavaScript 表达式时就不要再到外面套引号了.JSX 会将引号当中的内容识别为字符串而不是 ...

  7. java reflect反射---Java高级开发必须懂的

    理解反射对学习Java框架有很大的帮助,如Spring框架的核心就是使用Java反射实现的,而且对做一些Java底层的操作会很有帮助.  一.Class类的使用         1.万事万物皆对象,( ...

  8. 关于Action和EventHandler

    .net框架自带的两个常用类(Action和EventHandler),当然这两个类型的也可以自定义,但系统已经提供,直接拿来用即可,很方便 1:Action : 引用“void方法”的委托,目前框架 ...

  9. Selenium with Python 003 - 页面元素定位

    WebUI自动化,首先需要定位页面中待操作的元素,然后进行各种事件操作,这里我们首先介绍Selenium Python 如何定位页面元素,WebDriver 提供了一系列的方法. 定位单个页面元素(返 ...

  10. poj3311 状压dp+floyd

    先floyd预处理一遍dis,枚举所有状态,dp[ i ] [ j ]表示 以  j  为终点的状态 i 使用最小的时间 #include<map> #include<set> ...