题意:给出一个数列。问当中存在多少连续子区间,当中子区间的(最大值-最小值)<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. linq操作符:限定操作符

    限定操作符运算返回一个Boolean值,该值指示序列中是否有一些元素满足条件或者是否所有元素都满足条件. 一.All操作符 All方法用来确定是否序列中的所有元素都满足条件.看下面的例子: using ...

  2. Spring @Value注解问题

    xml配置了下面标签:<context:property-placeholder location="classpath:xxx.properties" /> 用spr ...

  3. 自定义shareSDK的验证码短信内容

    应用中使用了shareSDK来做第三方登录和短信验证码的接收,但是想将短信内容修改为自己想要的内容 官方文档中并未详细提及:无GUI接口调用 默认的短信内容为: 如果只是要修改括号中的抬头,只需按照此 ...

  4. 安卓程序代写 网上程序代写[原]BluetoothSocket详解

    一. BluetoothSocket简介 1. 简介 客户端与服务端 : BluetoothSocket 和 BluetoothServerSocket 类似于Java中的套接字的 Socket 和 ...

  5. 关于Unity中关节的使用(二)

    导入 1.创建工程 2.导入joint_scene.unitypackage 3.运行发现机械爪子摇来摇去,因为line节点下面的子节点之间相互碰撞带动的关系 4.为了消除这种乱摇,我们在右上角添加一 ...

  6. Numpy 用于数组的文件输入和输出

    将数组以二进制格式保存 np.save 和np.load 是读写磁盘数组数据的两个主要函数.默认情况下,数组是以未压缩的原始二进制格式进行保持在扩展名 为.npy的文件中的 如果文件路径末尾没有扩展名 ...

  7. WEB打印大全

    1.控制"纵打". 横打”和“页面的边距. (1)<script defer> function SetPrintSettings() {  // -- advance ...

  8. (笔记)Linux下查看CPU使用率的命令

    1.top 使用权限:所有使用者 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示process的动态 d :改变显示的更新速度,或 ...

  9. 容易出错的 if 语句

    下面列举几个容易出错的if语句实例,如果后续还有新的发现,还会继续更新! 出错一:在括起控制表达式的括号后面加分号 ; ); printf("值为正"); 初次运行,感觉一切正常, ...

  10. mysql 合理创建索引

    场景: KEY `index_gscode_f4_f7` (`gscode`,`f4`,`f7`) USING BTREE KEY `index_gscode_f7_f4` (`gscode`,`f7 ...