题意:给出一个数列。问当中存在多少连续子区间,当中子区间的(最大值-最小值)<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. android开发(40) 初试 Volley - GoogleI02013上的“快速,简单的网络通讯库”

    什么是Volley Google I/O 2013上,Volley发布了.Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮.这是Volley名称的由来: a burst ...

  2. vbs中的"WScript.Network"[属性与方法]

    属性ComputerName                  计算机名UserDomain                    所属局域网域的域名UserName                 ...

  3. python里面有人写while 循环用 用while 1 和while True的区别

    由于Python2中,True/False不是关键字,因此我们可以对其进行任意的赋值,这就导致程序在每次循环时都需要对True/False的值进行检查:而对于1,则被程序进行了优化,而后不会再进行检查 ...

  4. Matlab函数——std,std2与mean,mean2区别

    最近看代码,经常看到std,std2的用法,由于刚刚学习,网上搜索了下,没有找到解答,看了help,s = std(X,flag)s = std(X,flag,dim)  ,只告诉我们 flag,di ...

  5. 【转】为什么说 Java 程序员必须掌握 Spring Boot ?

    Spring Boot 2.0 的推出又激起了一阵学习 Spring Boot 热,那么, Spring Boot 诞生的背景是什么?Spring 企业又是基于什么样的考虑创建 Spring Boot ...

  6. 关于Unity中物理检测的准备

    1.要确定每个物体的碰撞类型,是有碰撞效果的碰撞还是没有碰撞效果的碰撞(is trigger),带不带刚体. 2.给每个物体分层,再设置哪些层会发生碰撞,哪些完全不产生碰撞. 3.给每个物体设置标记, ...

  7. 损失函数Center Loss 代码解析

    center loss来自ECCV2016的一篇论文:A Discriminative Feature Learning Approach for Deep Face Recognition. 论文链 ...

  8. java 5.0引入的新特性-枚举

    概念 首先,枚举并不是一种新技术,而是一种基础数据类型.它隶属于两种基础类型中的值类型,如下: 2.  为什么要有枚举 枚举在真正的开发中是非常常用的,它的作用很简单也很纯粹:它定义了一种规范,就是要 ...

  9. 使用ClaimsIdentity来实现登录授权

    背景:以前做登录时用的都是FormsAuthentication.SetAuthCookie(model.UID, IsRemeber),但是有一个不好,不能存储多个值,有时候我们既想存储登录用户的U ...

  10. CI框架 -- 核心文件 之 Lang.php(加载语言包)

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class CI_Lang { var $l ...