题意:

  给一个整数序列,多达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. ios Camera学习笔记

    检测设备的摄像头是否可用: - (BOOL) isCameraAvailable{ return [UIImagePickerController isSourceTypeAvailable: UII ...

  2. ubuntu中磁盘挂载与卸载

      问题描述:          ubuntu中磁盘的挂载和卸载     问题解决:          (1)ubuntu中磁盘挂载        注:    如上所示,使用命令df查看磁盘使用情况 ...

  3. iOS开发之数据存取3-CoreData自定义数据类型

    当系统提供的类型不能达到我们的使用要求时,比如我想在CoreData中存储UIColor,该怎么办呢? 这时候就要用到CoreData中非常强大的一个存储类型了:Transformable 下面将通过 ...

  4. 基于jQuery的TreeGrid组件详解

    一.TreeGrid组件相关的类 1.TreeGrid(_config) _config:json格式的数据,组件所需要的数据都通过该参数提供. 2.TreeGridItem(_root, _rowI ...

  5. Unity3D研究院之IOS全自动打包生成ipa

    接着上一篇文章, 自动生成framework,这篇文章我把shell自动化打包ipa整理了一下,希望大家喜欢,嘿嘿.. 建议大家先看一下上一篇文章.http://www.xuanyusong.com/ ...

  6. MariaDB Galera Cluster 部署(如何快速部署MariaDB集群)

    MariaDB Galera Cluster 部署(如何快速部署MariaDB集群) [日期:--] 来源:Linux社区 作者:Linux [字体:大 中 小] MariaDB作为Mysql的一个分 ...

  7. winform 开发之Control.InvokeRequired

    Control.InvokeRequired 获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中. InvokeRequir ...

  8. hdu 3661 Assignments(水题的解法)

    题目 //最早看了有点云里雾里,看了解析才知道可以很简单的排序过 #include<stdio.h> #include<string.h> #include<algori ...

  9. poj 3150 Cellular Automaton

    首先来看一下Sample里的第一组数据.1 2 2 1 2经过一次变换之后就成了5 5 5 5 4它的原理就是a0 a1 a2 a3 a4->(a4+a0+a1) (a0+a1+a2) (a1+ ...

  10. Android sqlite cursor的遍历

    查询并获得了cursor对象后,用while(corsor.moveToNext()){}遍历,当corsor.moveToNext()方法调用,如果发现没有对象,会返回false public Li ...