玲珑杯”ACM比赛 Round #19 B 维护单调栈
2024-10-15 06:16:48
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 ;
}
最新文章
- 关于ř与画面的集成---- k均值聚类
- 深入理解iOS开发中的BitCode功能
- C# lambda
- Druid.io索引过程分析——时间窗,列存储,LSM树,充分利用内存,concise压缩
- 【转】Nginx 安装配置
- PHP编译过程中常见错误信息的解决方法
- 在Windows Live Writer中插入C# code
- 搭建Windows Azure开发环境-Azure虚拟机
- Introspector(内省)简单演示样例 与 简单应用
- mac nodejs安装
- Layout基本属性总结
- 8Manage:数据安全,企业新时代的护航利器
- 为什么会有可恶的腾讯电脑管家&;怎么干掉它-电脑开机出现腾讯电脑管家-无法卸载腾讯电脑管家
- [ZJOI2008]生日聚会
- SUSE12SP3-Mycat(3)Server.xml配置详解
- 【CSS 第六天】三种简历
- template or render function not defined vue 突然报错了,怎么解决
- Difference Between Git and SVN
- Ubuntu Server 18.04 网络设置不生效的解决
- 信息摘要算法之四:SHA512算法分析与实现
热门文章
- redis 为什么快
- 两张神图介绍python3和 2.x与 3.x 的区别
- AJAX学习2
- Hybrid APP基础篇(三)->;Hybrid APP之Native和H5页面交互原理
- python 抓取网页(一)
- Scrum立会报告+燃尽图(十月二十九日总第二十次)
- 关于算法的时间复杂度O(f(n))
- HDU 5183 Negative and Positive (NP) 前缀和+哈希
- java结合testng,利用XML做数据源的数据驱动示例
- flink ha zk集群迁移实践