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. 我用.htaccess做了些什么?

    1.防图片盗链,减轻流量压力: 2.index.php 301转向到域名,有利于PR权重集中: 3.其它还不会,慢慢来…… 我是如何做的? <IfModule mod_rewrite.c> ...

  2. 原始套接字&&数据链路层访问

    1. 原始套接字能力: (1) 进程可以读写ICMP,IGMP等分组,如ping程序: (2) 进程可以读写内核不处理协议字段的ipv4数据报:如OSPF等: (3) 进程可以使用IP_HDRINCL ...

  3. jstorm相关

    https://www.cnblogs.com/antispam/p/4182210.html

  4. clearcase command (windows 常用的几个)

    command 1. setview  指定某个view你可以操作,否则你将看不到文件,ls,cd 等其它命令无效 setview  viewname 2. rename branch type na ...

  5. Leetcode 之Flatten Binary Tree to Linked List(50)

    将左子树接到右子树之前,递归解决 void flatten(TreeNode *root) { if (root == nullptr)return; flatten(root->left); ...

  6. java-增强for循环

    public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); lis ...

  7. 7:django 中间件

    中间件 中间件是一个连接django请求/相应处理的框架,是一个轻量级的低层次的全局影响django输入输出的系统插件. 每一个中间件组件负责一些特定的功能,这里我们我们只看一下如何激活使用系统自带的 ...

  8. 197. Rising Temperature

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  9. Ubuntu 18.04安装网易云音乐(转载)

    作为Ubuntu下唯一一款超级好用的音乐软件,必须下载. 提升为root权限后操作 0 : 网易云音乐1.0.0(该版本较为好安装)下载地址 http://s1.music.126.net/downl ...

  10. Ubuntu 18.04安装 Sublime

    作为一款强大的源代码编辑器,当然需要一手流利的命令来安装它. 命令: wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | s ...