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的更多相关文章

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

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

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

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

  3. HDU 5289 Assignment(二分+RMQ-ST)

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

  4. hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...

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

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

  6. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

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

  7. HDU 5289——Assignment——————【RMQ+优化求解】

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

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

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

  9. HDU 5289 Assignment (ST算法区间最值+二分)

    题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...

随机推荐

  1. mysql where/having区别

    mysql> select 2-1 as a,password from mysql.user where user='root' having a>0; +---+----------- ...

  2. 多表数据转化器MTDC

    需求 根据配置文件的映射规则,将一种模型和数据映射成另外一种模型和数据.如图: 其中,a1,b1,c1,d1为表主键,关系:A.a1=B.b1=C.c2=D.d1 解决思路 解析模型配置文件,将每个转 ...

  3. 再思linux内核在中断路径内不能睡眠/调度的原因(2010)【转】

    转自:http://blog.csdn.net/maray/article/details/5770889 Linux内核中断路径中不能睡眠,为什么? 这里就行了很深入的讨论,值得一看:http:// ...

  4. linux dpm机制分析(上)【转】

    转自:http://blog.csdn.net/lixiaojie1012/article/details/23707681 1      DPM介绍 1.1        Dpm:  设备电源管理, ...

  5. java中this的用法如:this.name=name

    package com.chensi; /** * 这个是为了搞懂那个 this.name = name的. * @author ZHL * */ public class ThisTestZhl { ...

  6. python基础(3)---流程控制

    流程控制 与C语言不通的是python的流程控制代码块不是用{}花括号表示的,而是强制缩进来控制的:而且缩进必须一致,官方推荐是使用4个空格,不建议使用tab(制表符)做缩进,一是不同的系统tab所占 ...

  7. K8s的内部Pod之间都不通,搞了快两天

    试了不亚于二十种方法,绝望的时候,回到了家. 想手工安装,又遇到flannel在手工下,会更改docker启动项的不完善. cni,或许就是k8s的大方向吧. 最后,抱着试一试的态度,将flannel ...

  8. lr_Controller界面图

    lr可用运行图介绍: lr集合点策略:

  9. mybatis 报错: Invalid bound statement (not found)

    错误: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): test.dao.Produc ...

  10. UVa 221 Urban Elevations 城市正视图 离散化初步 无限化有限

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 221 Urban Elevations 给出城市中建筑物的x, ...