题意:给出一个数列。问当中存在多少连续子区间,当中子区间的(最大值-最小值)<k

思路:设dp[i]为从区间1到i满足题意条件的解。终于解即为dp[n]。

此外 如果对于arr[i] 往左遍历 一直到arr[r] 此时从区间r到区间i满足(最大值-最小值)<k,再往左一位即越界 或者 不满足条件,此时有 dp[i]
= dp[i-1]
+ i - r
+ 1;

由于数据量大 往左遍历时 可能会超时 ,所以用rmq打表 查找r时用二分 就过了

代码:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define sss(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a) memset(a,0,sizeof(a))
#define ss(a,b) scanf("%d%d",&a,&b)
#define s(a) scanf("%d",&a)
#define INF 0x3f3f3f3f
#define w(a) while(a)
#define PI acos(-1.0)
#define LL long long
#define eps 10E-9
#define N 100010
using namespace std;
void mys(int& res) {
int flag=0;
char ch;
while(!(((ch=getchar())>='0'&&ch<='9')||ch=='-'))
if(ch==EOF) res=INF;
if(ch=='-') flag=1;
else if(ch>='0'&&ch<='9') res=ch-'0';
while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
res=flag?-res:res;
}
void myp(int a) {
if(a>9)
myp(a/10);
putchar(a%10+'0');
}
/********************the end of template********************/
int arr[N];
int sm[N][30], bg[N][30];
LL dp[N];
void rmq_init(int n) {
for(int i = 0; i < n + 1; i++) bg[i][0] = sm[i][0] = arr[i];
for(int j = 1; (1 << j) <= n + 1; j++) {
for(int i = 0; i + (1 << j) - 1 < n + 1; i++) {
bg[i][j] = max(bg[i][j - 1], bg[i + (1 << (j - 1))][j - 1]);
sm[i][j] = min(sm[i][j - 1], sm[i + (1 << (j - 1))][j - 1]);
}
}
}
int rmq_min(int left, int right){
int kk = 0;
w((1 << (kk+1)) <= right - left +1) kk++;
return min(sm[left][kk], sm[right - (1<<kk) +1][kk]);
}
int rmq_max(int left, int right){
int kk = 0;
w((1 << (kk+1)) <= right - left +1) kk++;
return max(bg[left][kk], bg[right - (1<<kk) +1][kk]);
}
int getR(int L, int k, int R) {
int l = L, r = R, m, ans;
while(l <= r) {
m = (l + r) >> 1;
int view = rmq_max(m, R) - rmq_min(m, R);
if(view < k) {
ans = m;
r = m - 1;
}
else l = m + 1;
}
return ans;
}
int main(){
int t;
s(t);
w(t--){
int n, k;
mem(dp);
ss(n, k);
for(int i=1; i<=n; i++){
s(arr[i]);
}
rmq_init(n);
dp[1] = 1;
for(int i=2; i<=n; i++){
int R = getR(1, k, i);
dp[i] = dp[i-1] + i - R + 1;
}
cout<<dp[n]<<endl;
}
return 0;
}

hdu5289 2015多校联合第一场1002 Assignment的更多相关文章

  1. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  2. hdu5294||2015多校联合第一场1007 最短路+最大流

    http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...

  3. HDU 5289 Assignment(多校联合第一场1002)

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

  4. 2015 多校赛 第一场 1002 (hdu 5289)

    Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n ...

  5. 2015 多校赛 第一场 1007 (hdu 5294)

    总算今天静下心来学算法.. Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the e ...

  6. HDU 4865 Peter's Hobby(2014 多校联合第一场 E)(概率dp)

    题意:已知昨天天气与今天天气状况的概率关系(wePro),和今天天气状态和叶子湿度的概率关系(lePro)第一天为sunny 概率为 0.63,cloudy 概率 0.17,rainny 概率 0.2 ...

  7. HDU 4868 Information Extraction(2014 多校联合第一场 H)

    看到这道题时我的内心是奔溃的,没有了解过HTML,只能靠窝的渣渣英语一点一点翻译啊TT. Information Extraction 题意:(纯手工翻译,有些用词可能在html中不是一样的,还多包涵 ...

  8. 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 ...

  9. HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场

    题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...

随机推荐

  1. asp.net cookie的操作

    //写入 protected void Button2_Click(object sender, EventArgs e)     {         HttpCookie cookie=new Ht ...

  2. mongoDB自动杀执行时间的连接

    ####shell 执行 mongo -u'root' -p'密码' admin mg.js [root@localhost ~]# cat mg.js var currOp = db.current ...

  3. Excel查看某列的重复值

    例如: 当查看的是B列的重复值时:=IF(COUNTIF(B:B,B1)>1,"重复","")

  4. svn出现skips remain conficted,不能更新代码问题

    出现: skips remain conficted One or more files are in a conflicted state 然后commit的时候出现,很多都已经deleted,但是 ...

  5. 关闭R语言载入包时候的警告

    options(warn =-1)

  6. Axure RP Pro 7.0 注册码

    用户名:aaa注册码:h624pifAqt7It5e8boKkML+Y4RjDX5xknP4k7QktJYQoxsvv7VUS7hBCv/2ef45P

  7. Android 8 Wifi 初始化过程

    记录一下wifi初始化过程. packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java public void on ...

  8. server的响应数据

    前言 如果使用了MVC框架(比方,struts2). server的响应数据.分3种情况 1.响应数据是结果页面 2.响应数据是json格式的数据 3.响应数据是json格式的数据,然后再又一次发出一 ...

  9. Jquery Gritter set position

    You can try... //css .gritter-center{ position:fixed; left:33%; right:33%; top:33% } //Javascript $. ...

  10. Linux入门教程:如何检查Linux系统的最后重启时间

    问题: 是否有一个命令可以快速地检查系统已经运行了多久? 也就是我怎么知道Linux系统最后的重启时间? 有许多方法来查询系统最后的重启时间. 方法一 第一种方法是使用last命令. $ last r ...