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. CDC之fast->slow (1)

    Sampling slower signals into faster clock domains causes fewer potential problems than sampling fast ...

  2. CMMI评估流程

    原文链接:http://www.cmmcn.com/new/cmmi-105.html 当前位置:首页 >> CMMI知识库 >> CMMI相关 >> CMMI评估 ...

  3. WebGL绘制三角形

    本文程序实现绘制一个三角形的任务,如下图. 整个程序包含两个文件,分别是: 1. HelloTriangle.html <!DOCTYPE HTML PUBLIC "-//W3C//D ...

  4. php 加密解密函数封装

    算法一: //加密函数 function lock_url($txt,$key='yang') { $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi ...

  5. java实现简单窗体小游戏----球球大作战

    java实现简单窗体小游戏----球球大作战需求分析1.分析小球的属性: ​ 坐标.大小.颜色.方向.速度 2.抽象类:Ball ​ 设计类:BallMain—创建窗体 ​ BallJPanel—画小 ...

  6. jmeter的性能监控框架搭建记录(Influxdb+Grafana+Jmeter)

    查看笔记 http://note.youdao.com/noteshare?id=c700365713abb98bd3d10e6f45393af9&sub=6F4E14FF3F9D4167AE ...

  7. python之testlink模块

    1.安装:pip install TestLink-API-Python-client >>>>>>待续

  8. nyoj4-ASCII码排序

    ASCII码排序 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符. 输入 第一行输入一个数N, ...

  9. PHP和zookeeper结合实践

    Zookeeper 简单介绍 Apache Zookeeper是开发和维护开源服务器的服务,它能够实现高度可靠的分布式协调. 安装Zookeeper(无需安装) wget http://mirror. ...

  10. SOA案例架构分析浅谈

    上课中讲到了SOA架构设计,自己在课下决定总结一下对于SOA架构的理解以及应用. 先总结一下SOA的定义,SOA是面向服务架构,它可以根据需求通过网络对松散耦合的粗粒度应用组件进行分布式部署.组合和使 ...