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. 【sqli-labs】 less36 GET- Bypass MYSQL_real_escape_string (GET型绕过MYSQL_real_escape_string的注入)

    看一下mysql_real_escape_string()函数 \x00 \x1a 注入的关键还是在于闭合引号,同样使用宽字节注入 http://192.168.136.128/sqli-labs-m ...

  2. sqlitManager

    @interface sqlitManager : NSObject +(instancetype)sharedSqlitManager; -(void)createDB; -(void)create ...

  3. 微信小程序—picker(滚动选择器)

    官方api:https://mp.weixin.qq.com/debug/wxadoc/dev/component/picker.html 上边是官网的api.小程序中,底部下拉滚动选择主要有这几种 ...

  4. 团体程序设计天梯赛-练习集-L1-039. 古风排版

    L1-039. 古风排版 中国的古人写文字,是从右向左竖向排版的.本题就请你编写程序,把一段文字按古风排版. 输入格式: 输入在第一行给出一个正整数N(<100),是每一列的字符数.第二行给出一 ...

  5. PAT_A1113#Integer Set Partition

    Source: PAT A1113 Integer Set Partition (25 分) Description: Given a set of N (>) positive integer ...

  6. kernel panic必备知识

    获得vmcore Kernel dump 是什么 Kdump – 捕捉kernel dump的工具 Kdump的工作原理 Kdump的配置 Dump分析的工具crash(1) 准备环境 根据vmcor ...

  7. 【JavaScript框架封装】自己动手封装一个涵盖JQuery基本功能的框架及核心源码分享(单文件版本)

    整个封装过程及阅读JQuery源码的过程基本上持续了一个月吧,最终实现了一个大概30%的JQuery功能的框架版本,但是里面涉及的知识点也是非常多的,总共的代码加上相关的注释大概在3000行左右吧,但 ...

  8. vue: This relative module was not found

    这是今天运行vue项目报的一个错误,特地在此记录一下. 错误信息如下: ERROR Failed to compile with 1 errors This relative module was n ...

  9. MYSQL(一) 简单语法

    MYSQL(一) 简单语法 1.默认约束:mysql里面DEFAULT关键字后面是不用加括号的 --1.1 创建数据库 mysql> create database holly; Query O ...

  10. 01010_Eclipse中项目的jar包导入与导出

    1.jar包 jar包是一个可以包含许多.class文件的压缩文件.我们可以将一个jar包加入到项目的依赖中,从而该项目可以使用该jar下的所有类:也可以把项目中所有的类打包到指定的jar包,提供给其 ...