题意:

  给一个整数序列,多达10万个,问:有多少个区间满足“区间最大元素与最小元素之差不超过k”。k是给定的.

思路:

  如果穷举,有O(n*n)复杂度。可以用ST算法先预处理每个区间最大和最小,O(nlogn)。再扫一遍整个序列,两个指针L,R用于这样判断:如果A[L,R]这个区间满足要求,则R++,否则统计ans+=R-L,且l++。因为在[L,R]这个区间是满足要求的,那么以L开头的,[L,L]、[L,L+1]...[L,R-1]就都是满足要求的,刚好R-L个。

 #include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=;
int a[N], cur;
int big[N][], small[N][]; void pre_cal(int n)
{
memset(big,,sizeof(big));
memset(small,,sizeof(small)); for(int i=; i<=n; i++)
{
big[i][]=a[i];
small[i][]=a[i];
} for(int i=,q=; i<=n; i<<=,q++) //以i为距离
{ for(int j=; j<=n; j++ )
{
if(j+i-<=n)
{
big[j][q]=max(big[j][q-],big[ j+i/ ][q-]);
small[j][q]=min(small[j][q-],small[ j+i/ ][q-]);
}
else break;
}
}
} unordered_map<int,int> mapp,mapp2;
void init() //获得二进制最高位,这个也可以先处理的。
{
for(int j=; j<=N; j++)
{
int tmp=, cnt=;
for(int i=; i<; i++)//找出二进制最高位的1
{
if(!(j>>i))
{
tmp=((j>>(i-))<<(i-));
break;
}
cnt++;
}
mapp2[j]=tmp;//记录j只剩最高位的值
mapp[j]=cnt;//记录tmp是2的几次幂
}
} bool iscor(int l, int r) //判断这个区间是否满足要求
{
int len=r-l+;
int da= max( big[l][ mapp[len] ], big[ r-mapp2[len]+ ][ mapp[len] ]);
int xiao=min( small[l][ mapp[len] ], small[ r-mapp2[len]+ ][ mapp[len] ]);
return da-xiao<cur? true :false; } int main(void)
{
freopen("e://input.txt", "r", stdin);
int t, n;
init();
cin>>t;
while(t--)
{
scanf("%d%d",&n,&cur);
for(int i=; i<=n; i++) scanf("%d",&a[i]);
pre_cal(n);
LL cnt=;
int l=, r=;
while(r<=n)
{
if( iscor(l,r) ) r++;
else
{
cnt+=r-l;
l++;
}
}
while(l<r) cnt+=r-l,l++;
printf("%lld\n",cnt);
}
return ;
}

AC代码

HDU 5289 Assignment (数字序列,ST算法)的更多相关文章

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

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

  2. HDU 5289 Assignment (ST算法区间最值+二分)

    题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...

  3. HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...

  4. HDU 5289 Assignment(二分+RMQ-ST)

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

  5. HDU 5443 The Water Problem (ST算法)

    题目链接:HDU 5443 Problem Description In Land waterless, water is a very limited resource. People always ...

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

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

  7. hdu 5289 Assignment (ST+二分)

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

  8. HDU 5289 Assignment rmq

    Assignment 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Description Tom owns a company and h ...

  9. HDU 5289 Assignment

    题意:求一段长度为n的序列里有多少个子区间内的最大值减最小值小于k. 解法:RMQ+尺取法或单调队列.RMQ可以用st或者线段树,尺取法以前貌似YY出来过……只是不知道是这个东西…… 设两个标记l和r ...

随机推荐

  1. Intellij IDEA14 下添加ExtJS提示支持

    前言: 虽然Interlij IDEA比起Eclipse对待EXT更为支持,但自己上手后总不能达到Intellij 开发ExtJS 应用指南(http://blog.csdn.net/s4640368 ...

  2. CentOS安装视频播放器SMPlayer

    首先下载rpmforg,下载对应的版本,就是对应CentOS版本,还有32位与64位也要对应上.地址如下: http://wiki.centos.org/AdditionalResources/Rep ...

  3. zju 1037 Gridland(找规律,水题)

    题目链接 多写几个案例,根据数据的奇偶性,就能找到规律了 #include<stdio.h> int main() { int t,n,m; double ans; scanf(" ...

  4. 【Apache运维基础(1)】Apache的安装与使用

    安装 yum -y install httpd httpd-devel # 在Ubuntu里面叫做Apache2,输入localhost能打开就算成功了 额...当然专业的运维还是老老实实的去编译吧; ...

  5. 一起学Maven

    转载自:http://blog.csdn.net/songdeitao/article/details/18452459 一. 初识Maven 开场白 在现在的项目开发过程中,越来越重视项目的管理,而 ...

  6. 自旋锁spin_lock和raw_spin_lock

    自旋锁spin_lock和raw_spin_lock Linux内核spin_lock.spin_lock_irq 和 spin_lock_irqsave 分析 http://blog.csdn.ne ...

  7. 每用户订阅上的所有者 SID 不存在 (异常来自 HRESULT:0x80040207)

    出现这个问题是因为pQueryFilter.WhereClause = "RoomNumber=" +cmbFromPoint.SelectedItem;中的cmbFromPoin ...

  8. 对象的继承关系在数据库中的实现方式和PowerDesigner设计

    原文:对象的继承关系在数据库中的实现方式和PowerDesigner设计 在面向对象的编程中,使用对象的继承是一个非常普遍的做法,但是在关系数据库管理系统RDBMS中,使用的是外键表示实体(表)之间的 ...

  9. JDK,JRE,JVM区别与联系

    JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...

  10. 1401 - Remember the Word

    注意到单词的长度最长100,其实最糟糕复杂度应该能到O(300005*100),需要注意的是在字典树上匹配单词时,一旦不匹配,则后面的就不会匹配,需要break出来(这个害我TLE查了半天,日!),还 ...