题意:

  给一个整数序列,多达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. git Clone SSL certificate problem: self signed certificate

    自己的git服务器遇到证书是自签的,git验证后会拒绝,此时,采用如下命令临时禁用就好 git -c http.sslVerify=false clone https://domain.com/pat ...

  2. 【转载】CCombobox使用大全

    一.如何添加/删除Combo Box内容 1. 在Combo Box控件属性的Data标签里面添加,一行表示Combo Box下拉列表中的一行.换行用ctrl+回车. 2. 在程序初始化时动态添加 如 ...

  3. 使用EF code first和asp.net mvc4遇到的问题总结

    最近使用EF code first和asp.net mvc4做项目,遇到些问题,记录一下. 一.EF code first 生成外键列问题. 一般情况下,都是先写一个int型外键id属性,然后写一个外 ...

  4. PHPStorm 3.0 与服务器端代码同步配置

    首先打开PhpStrom->Toolbar->Settings(工具栏的倒数第三行) 进入Settings后的界面如下 单击进入左边Deployment目录,在右边配置信息里面进行配置 填 ...

  5. 【leetcode】Median of Two Sorted Arrays(hard)★!!

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  6. spark在eclipse上配置

    环境:spark1.4.0,hadoop2.6.0 1.安装好jdk 2.在spark的conf目录下找到spark-env.sh.template,打开,在后面加上 export SCALA_HOM ...

  7. 20. atoi函数

    /* 输入一个表示整数的字符串,把该字符串转换成整数并输出 */ #include<iostream> #include<string> using namespace std ...

  8. 【Linux高频命令专题(18)】tail

    概述 tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但 ...

  9. 深入浅出Mybatis-分页

    http://blog.csdn.net/hupanfeng/article/details/9265341 http://blog.csdn.net/isea533/article/details/ ...

  10. MySQL登录报错"Access denied for user 'root'@'localhost' (using password: YES)"

    最近登录MySQL时候总报错: # mysql -uroot -p Enter password: ERROR (): Access denied for user 'root'@'localhost ...