题目链接:

pid=5289">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): 672    Accepted Submission(s): 335

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
 

5
28
Hint
First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]
 
Author
FZUACM
 
Source

解题:

比赛的时候,怎么想都想不正确。想去找近期的不合法的点,复杂度太高。

看了题解才知道是用ST算法的。先前不知道,这是一篇非常不错的ST算法的介绍。

http://blog.csdn.net/david_jett/article/details/46990651

枚举左边端点,二分右端点。用ST算法推断该区间是否合法,直至右端点到极限(即二分的左右边界相遇或交叉)。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#define mod 1000000007
using namespace std;
int t,n,k;
int a[100100],minn[100010][20],maxn[100010][20],mid;
long long ans;
void Rmq_Init()
{
int m=19;
for(int i=1;i<=n;i++)
maxn[i][0]=minn[i][0]=a[i];
for(int i=1;i<=m;i++)
for(int j=n;j;j--)
{
maxn[j][i]=maxn[j][i-1];
minn[j][i]=minn[j][i-1];
if(j+(1<<(i-1))<=n)
{
maxn[j][i]=max(maxn[j][i],maxn[j+(1<<(i-1))][i-1]);
minn[j][i]=min(minn[j][i],minn[j+(1<<(i-1))][i-1]);
}
}
}
int Query_dif(int l,int r)
{
int m=floor(log((double)(r-l+1))/log(2.0));
int Max=max(maxn[l][m],maxn[r-(1<<m)+1][m]);
int Min=min(minn[l][m],minn[r-(1<<m)+1][m]);
return Max-Min;
}
int solve(int l)
{
int le,ri;
le=l;
ri=n;
while(le<=ri)
{
mid=(le+ri)/2;
if(Query_dif(l,mid)>=k)
{
ri=mid-1;
}
else
{
le=mid+1;
}
}
/*if(Query_dif(l,mid)>=k)
return mid-l;
else
return mid-l+1;*/
return ri-l+1;
}
int main()
{
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
Rmq_Init();
for(int i=1;i<=n;i++)
{
ans=ans+solve(i);
//cout<<i<<" "<<Query_dif(i,n)<<endl;
//cout<<ans<<endl;
}
printf("%lld\n",ans);
}
return 0;
}

HDU 5289 Assignment (ST算法区间最值+二分)的更多相关文章

  1. hdu 5289 Assignment (ST+二分)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

  2. RMQ st算法 区间最值模板

    #include<bits/stdc++.h> ; ; int f[N][Logn],a[N],lg[N],n,m; int main(){ cin>>n>>m; ...

  3. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

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

  4. hdu3486 ST表区间最值+二分

    还是挺简单的,但是区间处理的时候要注意一下 #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  5. HDU 5289 Assignment [优先队列 贪心]

    HDU 5289 - Assignment http://acm.hdu.edu.cn/showproblem.php?pid=5289 Tom owns a company and he is th ...

  6. HDU 5289 Assignment (数字序列,ST算法)

    题意: 给一个整数序列,多达10万个,问:有多少个区间满足“区间最大元素与最小元素之差不超过k”.k是给定的. 思路: 如果穷举,有O(n*n)复杂度.可以用ST算法先预处理每个区间最大和最小,O(n ...

  7. HDU 5289 Assignment (二分+区间最值)

    [题目链接]click here~~ [题目大意]: 给出一个数列,问当中存在多少连续子序列,子序列的最大值-最小值<k [思路]:枚举数列左端点.然后二分枚举右端点,用ST算法求区间最值.(或 ...

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

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

随机推荐

  1. 6.11---@RequestMapping注解的6+2个属性---6.11

    produces:它的作用是指定返回值类型,不但可以设置返回值类型还可以设定返回值的字符编码: consumes: 指定处理请求的提交内容类型(Content-Type),例如application/ ...

  2. XML解析-Dom4j的DOM解析方式更新XML

    Dom4j工具,是非官方的,不在jdk中. 使用步骤: 1)导入dom4j的核心包. dom4j-1.6.1.jar 2)编写Dom4j读取xml文件的代码 1.更新XML 1.1.写出内容到xml文 ...

  3. 研磨JavaScript系列(五):奇妙的对象

    在JavaScript中,只有object和function两种东西有对象化的能力.我们先来说说函数的对象化能力. 任何一个函数都可以为其动态地添加或去除属性,这些属性可以是简单类型,可以是对象,也可 ...

  4. 【Caffe】Ubuntu16.04上配置安装caffe(Only CPU)

    一.首先看看自己的系统,Ubuntu16.04,cpu,没有Nvidia,没有opencv 二.安装依赖包 安装protobuf,leveldb,snappy,OpenCV,hdf5, protobu ...

  5. shouldComponentUpdate不能直接比较object

    凡是参阅过react官方英文文档的童鞋大体上都能知道对于一个组件来说,其state的改变(调用this.setState()方法)以及从父组件接受的props发生变化时,会导致组件重渲染,正所谓&qu ...

  6. 关于JS闭包的一点理解

    通常来讲,闭包通常是指函数内部可以访问到外部作用域的一个过程. 一.广义的定义:任何函数都产生了闭包. 二.狭义的定义:函数内部能访问到其他变量函数的作用域. 我们来看个例子 var a = 10; ...

  7. Django - 获取表单数据的三种方式

    1.query set 对象 2.字典 3.query set 元组 备注:对象通过 ”对象.列名"方式访问,元组通过“对象.索引”方式访问.

  8. 1、DataGridView

    DataGridView赋值后 通过RowPostPaint事件绘制行号 private void AddXh() { DataGridViewTextBoxColumn col = new Data ...

  9. sysbench_mysql

    ref http://seanlook.com/2016/03/28/mysql-sysbench/ 测试 当执行下面这个sysbench测试mysql的时候,你不知道的可能可能是: 这到底是在测试读 ...

  10. Secret of Chocolate Poles (Aizu1378——dp)

    Select Of Chocolate Poles 题意:有一个竖直放置的高度为l cm的盒子,现在有三种方块分别为1cm的白块,1cm的黑块,k cm的黑块,要求第一块放进去的必须是黑色的,盒子最上 ...