1149 - Buildings

Time Limit:2s Memory Limit:128MByte

Submissions:588Solved:151

DESCRIPTION

There are nn buildings lined up, and the height of the ii-th house is hihi.

An inteval [l,r][l,r](l≤r)(l≤r) is harmonious if and only if max(hl,…,hr)−min(hl,…,hr)≤kmax(hl,…,hr)−min(hl,…,hr)≤k.

Now you need to calculate the number of harmonious intevals.

INPUT
The first line contains two integers n(1≤n≤2×105),k(0≤k≤109)n(1≤n≤2×105),k(0≤k≤109). The second line contains nn integers hi(1≤hi≤109)hi(1≤hi≤109).
OUTPUT
Print a line of one number which means the answer.
SAMPLE INPUT
3 1
1 2 3
SAMPLE OUTPUT
5
HINT
Harmonious intervals are: [1,1],[2,2],[3,3],[1,2],[2,3][1,1],[2,2],[3,3],[1,2],[2,3].
题意:给你一个长度为n的序列 问有多少区间 使得 区间最大值-区间最小值<=k
题解:单调栈处理出以a[i]为最小值的区间左界右界  组合出合法的区间 注意 (同一左界右界)或者称为同一块 下的最小值可能会有重复
从左向右遍历时 将当前值的左界改为(同一块中上一个相同值的位置+1) 具体看代码;
 #pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define ll long long
#define mod 1000000007
using namespace std;
ll n,k;
ll a[];
ll l[],r[];
map<ll,map<ll,ll>> mp;
int main()
{
scanf("%lld %lld",&n,&k);
for(int i=; i<=n; i++)
scanf("%lld",&a[i]);
a[]=-1ll;
a[n+]=-1ll;
l[]=1ll;
for(int i=; i<=n; i++) //关键********
{
ll temp=i-;
while(a[temp]>=a[i])//维护一个递增的序列
temp=l[temp]-;
l[i]=temp+;
}
r[n]=n;
for (int i=n-; i>=; i--)
{
ll temp=i+;
while(a[temp]>=a[i])
temp=r[temp]+;
r[i]=temp-;
}
ll ans=;
for(int i=; i<=n; i++)
{
ll x=,y=;
if(mp[l[i]][r[i]]!=){//去重 更改l[i]
ll now=l[i];
l[i]=mp[l[i]][r[i]];
mp[now][r[i]]=i+;
}
else
mp[l[i]][r[i]]=i+;
for(int j=i-; j>=l[i]; j--)
{
if((a[j]-a[i])<=k)
x++;
else
break;
}
for(int j=i+; j<=r[i]; j++)
{
if((a[j]-a[i])<=k)
y++;
else
break;
}
ans=ans+x*y+x+y+1ll;
}
printf("%lld\n",ans);
return ;
}
 

玲珑杯”ACM比赛 Round #19 B 维护单调栈的更多相关文章

  1. “玲珑杯”ACM比赛 Round #19题解&源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】

    A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT ...

  2. “玲珑杯”ACM比赛 Round #19 B -- Buildings (RMQ + 二分)

    “玲珑杯”ACM比赛 Round #19 Start Time:2017-07-29 14:00:00 End Time:2017-07-29 16:30:00 Refresh Time:2017-0 ...

  3. “玲珑杯”ACM比赛 Round #19

    A -- A simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 DESCRIPTIO ...

  4. lonlifeOJ1152 “玲珑杯”ACM比赛 Round #19 概率DP

    E -- Expected value of the expression DESCRIPTION You are given an expression: A0O1A1O2A2⋯OnAnA0O1A1 ...

  5. “玲珑杯”ACM比赛 Round #1

    Start Time:2016-08-20 13:00:00 End Time:2016-08-20 18:00:00 Refresh Time:2017-11-12 19:51:52 Public ...

  6. “玲珑杯”ACM比赛 Round #12题解&源码

    我能说我比较傻么!就只能做一道签到题,没办法,我就先写下A题的题解&源码吧,日后补上剩余题的题解&源码吧!                                     A ...

  7. “玲珑杯”ACM比赛 Round #18

    “玲珑杯”ACM比赛 Round #18 Start Time:2017-07-15 12:00:00 End Time:2017-07-15 15:46:00 A -- 计算几何你瞎暴力 Time ...

  8. “玲珑杯”ACM比赛 Round #1 题解

    A:DESCRIPTION Eric has an array of integers a1,a2,...,ana1,a2,...,an. Every time, he can choose a co ...

  9. 玲珑杯”ACM比赛 Round #4 1054 - String cut 暴力。学到了扫描的另一种思想

    http://www.ifrog.cc/acm/problem/1054 问删除一个字符后的最小循环节是多少. 比赛的时候想不出,不知道怎么暴力. 赛后看了别人代码才晓得.唉,还以为自己字符串还不错, ...

随机推荐

  1. Java Basic&Security Tools

    JDK Tools and Utilities Basic Tools These tools are the foundation of the JDK. They are the tools yo ...

  2. MySQL-MMM方案

    参考文档: 官方文档:http://mysql-mmm.org/mmm2:guide 本文对mmm方案做简单介绍,并做1个简单的验证. 一.MySQL-MMM方案 1. MMM方案简介 MMM(Mul ...

  3. 在Windows2008下添加iscsi存储出现磁盘Offine(The disk is offine because of policy set by an adminstrator)的解决方法

    打开CMD命令行输入如下命令: DISKPART.EXE DISKPART> san SAN Policy : Offline Shared DISKPART> san policy=On ...

  4. NO.1:自学python之路------Hello world、判断、循环

    引言 人工智能如今越来越贴近生活,在这里将记录我自学python与tensorflow的过程.编程使用IDE:visual studio 2017,python版本3.6.4,tensorflow版本 ...

  5. Web安全入门书籍推荐

    <Web应用安全权威指南> 链接:https://pan.baidu.com/s/1wZgv3c9jhWm1bAUHZw3gEg 提取码:teqj <黑客攻防技术宝典__Web实战篇 ...

  6. 关于jsonp跨域的 实现

    1.实现原理    1.把接口写在 script标签的src 中 这个接口就可以访问(不会存在跨域问题  因为接口在浏览器地址栏是可以访问的  会返回json字符串); 2.直接写不可以  因为正常情 ...

  7. Java throw try catch

    public class Runtest { public static void main(String[] args) { // TODO Auto-generated method stub T ...

  8. sed条件不修改匹配

    sed '/^echo/!s/text/subtext/g' 如果是以echo开始行首的行就不进行替换. 参考sed substitution conditional

  9. 操作系统作业一——仿CMD

    实验一.CMD实验 2014商软2  卓宇靖  4238 一.        实验目的 (1)掌握命令解释程序的原理: (2)掌握简单的DOS调用方法: (3)掌握C语言编程初步. 二.        ...

  10. 201621123037 《Java程序设计》第11周学习总结

    作业11-多线程 标签(空格分隔): Java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程 ...