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. 使用performance进行前端性能监控

    该文章仅作为自己的总结 1.performance.timing对象 navigationStart:当前浏览器窗口的前一个网页关闭,发生unload事件时的Unix毫秒时间戳.如果没有前一个网页,则 ...

  2. oracle数据库之组函数

    组函数也叫聚合函数,用来对一组值进行运算,并且可以返回单个值 常见的组函数: (1)count(*),count(列名)  统计行数:找到所有不为 null 的数据来统计行数 (2)avg(列名)  ...

  3. Erlang/Elixir: 使用 OpenCV, Python 搭建图片缩略图服务器

    这篇文章是在OSX上测试和运行的的, Ubuntu下的安装和配置请移步到这里 应用程序进程树, 默认 Poolboy 中初始化10个用于处理图片的 Python 工作进程(Worker) 首先安装Op ...

  4. 3.Airflow使用

    1. airflow简介2. 相关概念2.1 服务进程2.1.1. web server2.1.2. scheduler2.1.3. worker2.1.4. celery flower2.2 相关概 ...

  5. 创建hive与hbase关联的hive表与hbase表

    创建hive与hbase的关联表 create external table hive_hbase(rowkey string,name string,addr string,topic string ...

  6. BVT、EVT、DVT、PVT产品开发几个阶段

      EVT EVT(Engineering Verification Test) 工程验证 产品开发初期的设计验证.设计者实现样品时做初期的测试验证,包括 功能和安规测试,一般由 RD(Researc ...

  7. nginx原声方法按照每天日志切割保存

    首先配置日志变量,然后配置日志 在/etc/nginx/conf.d/default.conf 配置变量 server{ if ($time_iso8601 ~ "^(\d{4})-(\d{ ...

  8. Linux java项目冲突不能正常运行

    部署项目,在windows环境部署到Tomcat能够正常运行,部署到Linux环境下只能访问一些html资源,不能正常运行. 解决步骤: 1.清除webapps目录下所有文件夹,将war包上传至web ...

  9. Base64编码图片存取与前台显示

    需求:将Base64编码图片以BLOB类型存入数据库,需要时取出显示 后台: String base64str=new String(log.getRequest_imgdata());//log为实 ...

  10. 7. I/O复用

    一.I/O复用的特点 能同时监听多个文件描述符 自身是阻塞的 当多个文件描述符同时就绪时,如果不采取额外的措施,程序就只能按顺序依次处理其中的每一个文件描述符 由于其第三个特点,所以服务器程序看起来仍 ...