HDU - 5289:Assignment(单调队列||二分+RMQ||二分+线段树)
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||二分+线段树)的更多相关文章
- HDU 5289 Assignment(单调队列)
题意:给T足数据,然后每组一个n和k,表示n个数,k表示最大同意的能力差,接下来n个数表示n个人的能力,求能力差在k之内的区间有几个 分析:维护一个区间的最大值和最小值,使得他们的差小于k,于是採用单 ...
- HDOJ 5289 Assignment 单调队列
维护一个递增的和递减的单调队列 Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- BZOJ.1758.[WC2010]重建计划(分数规划 点分治 单调队列/长链剖分 线段树)
题目链接 BZOJ 洛谷 点分治 单调队列: 二分答案,然后判断是否存在一条长度在\([L,R]\)的路径满足权值和非负.可以点分治. 对于(距当前根节点)深度为\(d\)的一条路径,可以用其它子树深 ...
- HDU 5289 Assignment [优先队列 贪心]
HDU 5289 - Assignment http://acm.hdu.edu.cn/showproblem.php?pid=5289 Tom owns a company and he is th ...
- HDU 5289 Assignment
题意:求一段长度为n的序列里有多少个子区间内的最大值减最小值小于k. 解法:RMQ+尺取法或单调队列.RMQ可以用st或者线段树,尺取法以前貌似YY出来过……只是不知道是这个东西…… 设两个标记l和r ...
- hdu 5266 pog loves szh III(lca + 线段树)
I - pog loves szh III Time Limit:6000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I ...
- HDU 2795 Billboard(宣传栏贴公告,线段树应用)
HDU 2795 Billboard(宣传栏贴公告,线段树应用) ACM 题目地址:HDU 2795 Billboard 题意: 要在h*w宣传栏上贴公告,每条公告的高度都是为1的,并且每条公告都要 ...
- HDU - 5289 Assignment (RMQ+二分)(单调队列)
题目链接: Assignment 题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...
- HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...
随机推荐
- 学习地址 hadoop生态圈
http://my.oschina.net/leejun2005/blog/140462 http://www.codelast.com/?p=3621&cpage=1#comment-361 ...
- Nested DollsHDU1677
/*题意:有n个矩形,用长和宽表示,如果一个的长和宽都比另一个小,那么这个嵌放在另一个中 所以先对w从大到小排序,w一样的按h从小到大排序,那么就从后面的箱子往前找,只要前面找到一个人h比自己大的就放 ...
- Java:判断字符串中包含某字符的个数
Java:判断字符串中包含某字符的个数 JAVA中查询一个词在内容中出现的次数: public int getCount(String str,String key){ if(str == null ...
- Python3.x:PDFMiner3k在线、本地解析pdf
Python3.x:PDFMiner3k在线.本地解析pdf 安装 pip install pdfminer3k 示例一:在线解析pdf ''' Demo:pdf2htmlex解析pdf Dateti ...
- MySQL java连接被拒绝:java.sql.SQLException: Access denied for user 'root'@'****' (using password: YES)
//系统运行出现错误:java.sql.SQLException: Access denied for user 'root'@'***.**.**.**' (using password: YES) ...
- 一图看懂join、left join、right join、fulljoin间的区别
INNER JOIN 关键字在表中存在至少一个匹配时返回行. LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配.如果右表中没有匹配,则结果为 NULL. ...
- 大家来聊聊如何PASS 360
真正做到PASS 360,启动项什么的.我觉得还是有可能的把? 虽然它就是一个流氓,但是我们要想办法比它更流氓. 只是有一个点:那就是行为监控,那既然行为监控(云分析抛开).那就找一个可信任的程序来调 ...
- python 图像库PIL详解
PIL详细文档 The most important class in the Python Imaging Library is the Image class, defined in the mo ...
- python 邮件发送实例
#!/usr/bin/env python # -*- coding: utf-8 -*- from email.header import Header from email.mime.text i ...
- 用requests库爬取猫眼电影Top100
这里需要注意一下,在爬取猫眼电影Top100时,网站设置了反爬虫机制,因此需要在requests库的get方法中添加headers,伪装成浏览器进行爬取 import requests from re ...