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. 不相交集合ADT -数据结构(C语言实现)

    读数据结构与算法分析 不相交集合 等价关系 满足三个性质 - 自反性 - 对称性 - 传递性 基本数据结构 基本思路 使用一个数组,下标表示该集合,内容表示指向的父亲 实现 类型声明 typedef ...

  2. selenium 标签页切换

    from selenium import webdriver import time browser=webdriver.Chrome() browser.maximize_window() # 窗口 ...

  3. 高可用Kubernetes集群-9. 部署kubelet

    十一.部署kubelet 接下来两个章节是部署Kube-Node相关的服务,包含:kubelet,kube-proxy. 1. TLS bootstrap用户授权 # kubelet采用TLS Boo ...

  4. 从零开始的Python学习Episode 7——文件基本操作

    文件基本操作 一.打开文件 f = open('11','r')#open('file path','mode') 创建一个文件对象 文件有多种打开模式: 1. 'r':新建一个文件对象以只读方式打开 ...

  5. MYSQL报警:Warning: Using a password on the command line interface can be insecure.

    问题描述:执行下面的语句,sql是执行成功了,但是出现了一个报警,报警看上去始终不舒服 mysql -hip -Pport -uuser -ppassword -e "use db;dele ...

  6. ARP 攻击

    场景 A攻击者 192.168.1.3 00:00:00:00:00:01 B受害者 192.168.1.2 00:00:00:00:00:02 C路由器 192.168.1.1 00:00:00:0 ...

  7. 使用java中的注解@see

    缘起 在写java时,有时需要写注释,而为了更好的描述,需要引用和参考其他代码.为了让阅读者更好的体验,javadoc中支持链接跳转,这就需要用到注解@see. @see用法 注解@see可以在注释中 ...

  8. 0422 寻找数学口袋精灵BUG

    首先要部署这个app项目就是第一步: 一.前提下载并安装JDK 在线图解:手把手教你安装JDK      http://www.lvtao.net/server/windows-setup-jdk.h ...

  9. 0302思考&回答

    看完这两个网页,我们可以看出it行业始终是一门热门行业,在现在这个人潮汹涌的人才市场,面对严峻的就业形势,我们应该拿什么去参见招聘?人多而工作职位有限,这警醒我们必须拥有一技之长,否则则会被淘汰.如果 ...

  10. NLP 入门

    作者:微软亚洲研究院链接:https://www.zhihu.com/question/19895141/answer/149475410来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...