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]

题意:给出数列长度 n 以及数字 k 以及数列,求出数列中所以满足其中最大值和最小值之差小于 k 的小数列的个数。注:单个数也算一个小数列且必然符合条件。

思路:

首先是查询区间最大值与最小值的方式——可以用树状数组,也可以用线段树,sparse-table由于空间限制不会用。

关于三者的特性——

st算法的复杂度 O(nlog(n)) / O(1) , 线段树为 O(nlog(n)) / (log(n)),树状数组 O(<nlog(n)) / O(log(n))

空间复杂度 st 为 O(nlog(n)), 线段树 O(n),常数较大 , 树状数组是 O(n)

编程上 st 和 树状数组 都比较容易实现,线段树代码较长

另外线段树灵活性较大

(引用自http://www.cnblogs.com/ambition/archive/2011/04/06/bit_rmq.html)

而后是遍历方式——从数列第一个数开始,并设指针 p 为1。如满足max-min<k,则++p,直到条件不满足。则此时有ans+=p-1(包含第一个数的所有组合)。之后第二个数同理,沿用指针 p ,因若第一个数满足条件,则第二个数也必然满足条件,以此类推。

遍历有小小的优化是,在判断++p后满足条件与否时,没必要调用query函数,直接以a[p]更新tmax和tmin即可。

优化后快 600ms。

代码如下:

(注意ans要用long long不然会wa。。)

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int maxn=;
  7. int n,k,t,a[maxn],maxval[maxn],minval[maxn],tmax,tmin;
  8. int lowbit(int x){
  9. return x&(-x);
  10. }
  11. void ini(){
  12. for(int i=;i<=n;i++){
  13. maxval[i]=minval[i]=a[i];
  14. for(int j=;j<lowbit(i);j<<=){
  15. maxval[i]=max(maxval[i],maxval[i-j]);
  16. minval[i]=min(minval[i],minval[i-j]);
  17. }
  18. }
  19. }
  20. void query(int l,int r){
  21. tmax=tmin=a[r];
  22. while(true){
  23. tmax=max(tmax,a[r]);
  24. tmin=min(tmin,a[r]);
  25. if(r==l) break;
  26. for(r-=;r-l>=lowbit(r);r-=lowbit(r)){
  27. tmax=max(tmax,maxval[r]);
  28. tmin=min(tmin,minval[r]);
  29. }
  30. }
  31. }
  32. int main(){
  33. //freopen("in.txt","r",stdin);
  34. scanf("%d",&t);
  35. while(t--){
  36. memset(maxval,,sizeof(maxval));
  37. memset(minval,0x3f,sizeof(minval));
  38. scanf("%d%d",&n,&k);
  39. for(int i=;i<=n;i++)
  40. scanf("%d",&a[i]);
  41. ini();
  42. int p=;
  43. long long ans=;
  44. for(int i=;i<=n;i++){
  45. if(p>n) p=n;
  46. query(i,p);
  47. while(tmax-tmin<k&&p<=n){
  48. p++;
  49. tmax=max(tmax,a[p]);
  50. tmin=min(tmin,a[p]);
  51. }
  52. ans+=p-i;
  53. }
  54. printf("%I64d\n",ans);
  55. }
  56. return ;
  57. }

2015 多校赛 第一场 1002 (hdu 5289)的更多相关文章

  1. 2015 多校赛 第一场 1007 (hdu 5294)

    总算今天静下心来学算法.. Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the e ...

  2. 2015 多校赛 第一场 1001 (hdu 5288)

    Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l&l ...

  3. 2015 多校赛 第二场 1002 (hdu 5301)

    Description Your current task is to make a ground plan for a residential building located in HZXJHS. ...

  4. hdu5289 2015多校联合第一场1002 Assignment

    题意:给出一个数列.问当中存在多少连续子区间,当中子区间的(最大值-最小值)<k 思路:设dp[i]为从区间1到i满足题意条件的解.终于解即为dp[n]. 此外 如果对于arr[i] 往左遍历 ...

  5. 2015 多校赛 第二场 1006 (hdu 5305)

    Problem Description There are n people and m pairs of friends. For every pair of friends, they can c ...

  6. 2015 多校赛 第二场 1004 hdu(5303)

    Problem Description There are n apple trees planted along a cyclic road, which is L metres long. You ...

  7. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  8. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  9. HDU 5289 Assignment(多校联合第一场1002)

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

随机推荐

  1. Maya API编程快速入门

    一.Maya API编程简介 Autodesk® Maya® is an open product. This means that anyone outside of Autodesk can ch ...

  2. CorelDRAW中内置的视频教程在哪里?

    CorelDRAW中内置了很多教学内容和视频教程,可以帮助用户快速学习和掌握CorelDRAW的使用方法,创作出个性化的作品.很多小伙伴表示找不到软件自带学习视频,现在小编就来告诉你. 用户可以通过两 ...

  3. 怎么从传统的盒子思想转为Flex 布局(css)

    前端进化很快,总是有新的技术出来,开始可能有些人用惯了盒子模型的思想 依赖 display属性 + position属性 + float属性.这三大件.它对于那些特殊布局非常不方便 我们就来看看Fle ...

  4. canvas画弧线

    arc(x, y, radius, startRad, endRad, [anticlockwise]) 在Canvas画布上绘制以坐标点(x,y)为圆心.半么为radius的圆上的一段弧线.这段弧线 ...

  5. 记一次获得 3 倍性能的 go 程序优化实践,及 on-cpu / off-cpu 火焰图的使用

    转自:https://mp.weixin.qq.com/s/9IKaXeWTiiQTFlvZzxgsEA 记一次获得 3 倍性能的 go 程序优化实践,及 on-cpu / off-cpu 火焰图的使 ...

  6. elasticsearch 权威指南搜索阅读笔记(四)

    多索引多type搜索 分页搜索 每页5条 查询一到3页数据 第一页:http://127.0.0.1:9200/blogs2/product/_search?size=5&from=0 第二页 ...

  7. javascript--闭包与this

    理解javascript中的闭包与this http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html http: ...

  8. bootstrap checkbox

    在使用bootstrap库中的checkboxlistrow时,我想要依据checkbox是否至少选中了一个选项来确定页面的跳转,即须要在js中操作checkbox.这样就存在一个问题,一般的chec ...

  9. 动态内存管理---new&amp;delete

    动态内存管理 动态对象(堆对象)是程序在执行过程中在动态内存中用new运算符创建的对象. 因为是用户自己用new运算符创建的.因此也要求用户自己用delete运算符释放,即用户必须自己管理动态内存. ...

  10. 打造终端下mutt收发邮件环境(fbterm,fetchmail,msmtp,procmail,mutt)

    实现mutt下收发邮件须要安装,mutt,fbterm,fetchmail,msmtp,procmail 下面是各配置文件.在home文件夹下,隐私信息有马赛克... .muttrc : 当中Mail ...