HDU 5289 Assignment rmq
Assignment
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5289
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],…,an,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
题意
给你n个数,然后连续的,且这个区间内最大值减去最小值的差小于k的话,就可以算作一队。
问你一共有多少种分队的方法。
题解:
暴力枚举左端点位置,然后二分枚举右端点,用一个rmq维护一下就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
struct RMQ{
const static int RMQ_size = maxn;
int n;
int ArrayMax[RMQ_size][21];
int ArrayMin[RMQ_size][21];
void build_rmq(){
for(int j = 1 ; (1<<j) <= n ; ++ j)
for(int i = 0 ; i + (1<<j) - 1 < n ; ++ i){
ArrayMax[i][j]=max(ArrayMax[i][j-1],ArrayMax[i+(1<<(j-1))][j-1]);
ArrayMin[i][j]=min(ArrayMin[i][j-1],ArrayMin[i+(1<<(j-1))][j-1]);
}
}
int QueryMax(int L,int R){
int k = 0;
while( (1<<(k+1)) <= R-L+1) k ++ ;
return max(ArrayMax[L][k],ArrayMax[R-(1<<k)+1][k]);
}
int QueryMin(int L,int R){
int k = 0;
while( (1<<(k+1)) <= R-L+1) k ++ ;
return min(ArrayMin[L][k],ArrayMin[R-(1<<k)+1][k]);
}
void init(int * a,int sz){
n = sz ;
for(int i = 0 ; i < n ; ++ i) ArrayMax[i][0] = ArrayMin[i][0] = a[i];
build_rmq();
}
}solver;
int a[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,k;scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
solver.init(a,n);
long long ans = 0;
for(int i=0;i<n;i++)
{
int l = i,r = n-1,Ans=i;
while(l<=r)
{
int mid = (l+r)/2;
if(solver.QueryMax(i,mid)-solver.QueryMin(i,mid)<k)l=mid+1,Ans=mid;
else r=mid-1;
}
ans += (Ans-i+1);
}
cout<<ans<<endl;
}
return 0;
}
HDU 5289 Assignment rmq的更多相关文章
- HDU - 5289 Assignment (RMQ+二分)(单调队列)
题目链接: Assignment 题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...
- 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(二分+RMQ-ST)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...
- HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...
- HDU 5289 Assignment(2015 多校第一场二分 + RMQ)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 5289——Assignment——————【RMQ+优化求解】
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
- HDU 5289 Assignment (ST算法区间最值+二分)
题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...
随机推荐
- sqlmap参数说明
--delay 设置每隔几秒测试一次注入 --safe-url 设置sqlmap要访问的正常url --safe-freq 设置每测试多少条注入语句后才去访问safe-url --code 设置能正常 ...
- mysql 1366的错误 字符集错误解决方案
最近用mysqlalchmy的时候遇到了 sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1366, "Incorrec ...
- react项目中遇到的一些问题
推荐使用facebook官方构建工具create-react-app来创建React基础工程.(然而我还是手动构建) (路由)官方旧版本和V4的比较.https://github.com/ReactT ...
- Dev的js智能提示
VS中"工具"->"选项"->"文本编辑器"->"JavaScript"->"Inte ...
- [hadoop][会装]HBase集群安装--基于hadoop ha模式
可以参考部署HBase系统(分布式部署) 和基于无HA模式的hadoop下部署相比,主要是修改hbase-site .xml文件,修改如下参数即可: <property> <name ...
- C语言string转int
再不用atoi函数接口时,将字符串型转换成整型. 例如:将字符串型“158”转换成整型158 int String2Int(char * buff) { ; ; '; index++) { value ...
- redis的安装和php的redis扩展
一.redis的安装和配置 1.官方现在源码 https://redis.io/download 2.解压源码 tar zxvf redis-3.2.11.tar.gz 3.编译 make 编译 ...
- Zookeeper 入门第一篇
转载原文地址: ZooKeeper学习总结 第一篇:ZooKeeper快速入门 ZooKeeper学习总结 第二篇:ZooKeeper深入探讨 ZooKeeper学习第一期---Zookeeper简单 ...
- 常用SQL时间格式SQLServer中文版的默认的日期字段datetime格式是yyyy-mm-d
常用SQL时间格式 SQL Server中文版的默认的日期字段datetime格式是yyyy-mm-dd Thh:mm:ss.mmm 例如: select getdate() -- ::08.177 ...
- WordPress 一键置顶文章(推荐用SM Sticky Clicky Star)
在 WordPress入门 之 发布新文章和管理文章 中,倡萌已经简单提到可以在文章编辑界面或者快速编辑界面设置置顶文章,但是如果你想在后台文章列表中添加一键置顶文章的功能,不妨试试 Quick St ...