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。。)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
int n,k,t,a[maxn],maxval[maxn],minval[maxn],tmax,tmin;
int lowbit(int x){
return x&(-x);
}
void ini(){
for(int i=;i<=n;i++){
maxval[i]=minval[i]=a[i];
for(int j=;j<lowbit(i);j<<=){
maxval[i]=max(maxval[i],maxval[i-j]);
minval[i]=min(minval[i],minval[i-j]);
}
}
}
void query(int l,int r){
tmax=tmin=a[r];
while(true){
tmax=max(tmax,a[r]);
tmin=min(tmin,a[r]);
if(r==l) break;
for(r-=;r-l>=lowbit(r);r-=lowbit(r)){
tmax=max(tmax,maxval[r]);
tmin=min(tmin,minval[r]);
}
}
}
int main(){
//freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
memset(maxval,,sizeof(maxval));
memset(minval,0x3f,sizeof(minval));
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
ini();
int p=;
long long ans=;
for(int i=;i<=n;i++){
if(p>n) p=n;
query(i,p);
while(tmax-tmin<k&&p<=n){
p++;
tmax=max(tmax,a[p]);
tmin=min(tmin,a[p]);
}
ans+=p-i;
}
printf("%I64d\n",ans);
}
return ;
}

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. WebGL画点程序v1

    本文程序实现画一个点的任务,如下图.其中,点的位置直接给定("硬编码")在顶点着色器中. 整个程序包含两个文件,分别是: 1. HelloPoint1.html <!DOCT ...

  2. Android 链接 手机有关问题及解决方案

    我出现的问题: 这是我百度的解决方案:

  3. c#同步锁Monitor.Enter(T)

    protected static object MObjLock = new object();//同步锁 public string GetData(int mId) { Monitor.Enter ...

  4. 11.03 在外链接中用OR逻辑

    select e.ename,d.deptno,d.dname,d.locfrom dept d left join emp e on(d.deptno = e.deptnoand (e.deptno ...

  5. C# 共享页调用css

    @RenderSection("Styles", required: false) @section Styles{ }

  6. APICloud上啦加载下拉刷新模块

    apicloud有自带的上啦加载下拉刷新,当让也可以用第三方或者在模块库里面找一个使用 一.下拉刷新,一下代码写在 apiready = function (){} 里面 apiready = fun ...

  7. python tips:类的专有属性

    实例通常能够调用类的属性,但是有些属性是类专有的,实例无法调用. 实例调用方法时查找属性时,首先在自己的__dict__中找,找不到去类中找,在类中能够找到的属性都位于dir(cls)中,如果类的某些 ...

  8. VCSA服务重启命令

    Sphere Web Client界面的服务分别是: vmware-mbcs vmware-netdumper vmware-rbd-watchdog 分别执行命令确认,首先执行命令: service ...

  9. Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树

    Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...

  10. String使用方式详细总结

    1.用双引号创建 2.用new String方式创建 3.双引号相加创建 4.两个new String相加时 5.两个引用相加时 6.双引号加new String创建或者new String加双引号创 ...