题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289

Assignment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3175    Accepted Submission(s): 1457

Problem 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],…,a[n](0<=a[i]<=10^9),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

First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]

 
Author
FZUACM
 
Source
 
Recommend
We have carefully selected several similar problems for you:  5669 5668 5667 5666 5665 
 
题意:给你一个数列,再给你一个k,问存在多少个连续子序列使得子序列的最大最小值差值 小于 k.
题解:
贪心+ST表
枚举右端点,因为左端点一定是递增或不变的,所以遇到枚举的区间内的最大最小值差值 大于等于 k,就将左端点加1。然后每次统计个数即可。
注意:答案要开long long。
 #include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
int n,mn[MAXN][],mx[MAXN][],a[MAXN];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void ST()
{
int i,j;
for(i=;i<=n;i++)mn[i][]=mx[i][]=a[i];
for(j=;(<<j)<=n;j++)
{
for(i=;i+(<<j)-<=n;i++)
{
mn[i][j]=min(mn[i][j-],mn[i+(<<(j-))][j-]);
mx[i][j]=max(mx[i][j-],mx[i+(<<(j-))][j-]);
}
}
}
int Query(int l,int r)
{
int j;
for(j=;(<<j)<=(r-l+);j++);j--;
return max(mx[l][j],mx[r-(<<j)+][j])-min(mn[l][j],mn[r-(<<j)+][j]);
}
int main()
{
int T,k,i,left,right;
long long ans;
T=read();
while(T--)
{
n=read();k=read();
for(i=;i<=n;i++)a[i]=read();
ST();
left=;//左端点(左端点一定是单调递增的)
ans=;
for(right=;right<=n;right++)//枚举右端点
{
while(Query(left,right)>=k&&left<right)left++;//在枚举右端点的同时,移动左端点.每次改变右端点时,左端点只可能不变或向右移动.
ans+=((long long)right-left+1LL);//统计的子串为以右端点为最右端,最左端在 右端点->左端点 之间.
}
printf("%lld\n",ans);
}
fclose(stdin);
fclose(stdout);
return ;
}

Hdu 5289-Assignment 贪心,ST表的更多相关文章

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

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

  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. [BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆)

    [BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆) 题面 给出一个长度为n的序列,选k段长度在L到R之间的区间,一个区间的值等于区间内所有元素之的和,使得k个区间的值之和最大.区 ...

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

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

  5. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...

  6. HDU 5289 Assignment (数字序列,ST算法)

    题意: 给一个整数序列,多达10万个,问:有多少个区间满足“区间最大元素与最小元素之差不超过k”.k是给定的. 思路: 如果穷举,有O(n*n)复杂度.可以用ST算法先预处理每个区间最大和最小,O(n ...

  7. hdu 5289 Assignment (ST+二分)

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

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

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

  9. HDU 5289 Assignment rmq

    Assignment 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Description Tom owns a company and h ...

随机推荐

  1. 你不需要jQuery(三):新AJAX方法fetch()

    XMLHttpRequest来完成ajax有些老而过时了. fetch()能让我们完成类似 XMLHttpRequest (XHR) 提供的ajax功能.它们之间的主要区别是,Fetch API 使用 ...

  2. 分享SCI写作经验和一些工具

  3. jsp 获取表单值, 提交类型为multipart/form-data处理

    //tt.jsp<script type="text/javascript"> function doSubmit(){ alert("aaaaaa" ...

  4. Tiny6410 LED字符设备驱动

    1.查看用户手册 led1.led2.led3.led4 连接的分别是 GPK4.GPK5.GPK6.GPK7 2.查询6410芯片手册 下面还需要3个步骤: 1.设置GPIO为OUTPUT. 将GP ...

  5. php的redis 操作类,适用于单台或多台、多组redis服务器操作

    redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...

  6. POJ3283+字典树

    简单的字典树 /* 字典树 构造字典树.注意初始化! */ #include<stdio.h> #include<string.h> #include<stdlib.h& ...

  7. P66、面试题8:旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数 ...

  8. 24.allegro中光绘gerber[原创]

    光绘 一,基本设置 或者: ------------------------------------- ------- ------- 二,需要设置内容: 1)顶层和底层: 2)内部布线层: 3)OU ...

  9. Java面试题-多线程

    1. java中有几种方法可以实现一个线程? 多线程有两种实现方法,分别是继承Thread类与实现Runnable接口. 这两种方法的区别是,如果你的类已经继承了其它的类,那么你只能选择实现Runna ...

  10. vijos1603迷宫

    这题的构思太巧妙了: 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值    把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j. ...