题目: 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. 为Dapper编写一个类似于EF的Map配置类

    引言 最近在用Dapper处理Sqlite.映射模型的时候不喜欢用Attribute配置,希望用类似EF的Map来配置,所以粗略的实现了一个. 实现 首先是主体的配置辅助类型: using Syste ...

  2. bootstrap-treeview

    简要教程 bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一 ...

  3. PHP MSSQL数据操作PDO API

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  4. 如何使用 APM 搞定 PHP 应用的性能优化?

    APM 究竟是什么? 很多人都是第一次听说 APM 的概念,本文主要阐述如何使用 APM 的解决方案来实现 PHP 应用性能的优化.首先先介绍一下 APM (Application Performan ...

  5. Windows调试的基石——符号(1)

    当应用程序被链接以后,代码被逐一地翻译为一个个的地址,优化以后的代码可能初看起来更是面目全非.每当我们使用vs或者windbg等微软的调试工具进行调试的时候,我们可以方便地使用变量名来查看内存.可以使 ...

  6. SDUT2191Calendar

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2191 题意:给你两个年月日,让你算出其中经历了 ...

  7. hdu 4336 Card Collector 容斥原理

    读完题目就知道要使用容斥原理做! 下面用的是二进制实现的容斥原理,详见:http://www.cnblogs.com/xin-hua/p/3213050.html 代码如下: #include< ...

  8. Spring mvc 模式小结

    http://www.taobaotesting.com/blogs/2375 1.spring mvc简介 Spring MVC框架是一个MVC框架,通过实现Model-View-Controlle ...

  9. C++ 通过对象方式 、指针方式两种方式去访问成员变量(属性或者方法)

    准备 1.在VS中新建一个项目-Viscal C++ ---常规--空项目 2.建立一个.h的头文件 定义一个类 声明其成员(C#中的属性和方法) #include<iostream> # ...

  10. jstack(查看线程)、jmap(查看内存)和jstat(性能分析)

    公司内部同事分享的一篇文章 周末看到一个用jstack查看死锁的例子.昨天晚上总结了一下jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令.供大家参考 1.Jstack 1.1 ...