博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
“玲珑杯”ACM比赛 Round #19 B -- Buildings (RMQ + 二分)
阅读量:6944 次
发布时间:2019-06-27

本文共 1777 字,大约阅读时间需要 5 分钟。

Start Time:2017-07-29 14:00:00 End Time:2017-07-29 16:30:00 Refresh Time:2017-07-29 16:42:55 Private

B -- Buildings

Time Limit:2s Memory Limit:128MByte

Submissions:590Solved:151

DESCRIPTION

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

An inteval [l,r][l,r](lr)(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(1n2×105),k(0k109)n(1≤n≤2×105),k(0≤k≤109). The second line contains nn integers hi(1hi109)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].
【题意】给你一个序列,然后问你有多少个区间的最大值-最小值<=k。
【分析】我们可以用RMQ来logn查询区间最大、最小值,然后枚举每一位最为区间右端点,向左二分左端点,将区间长度加到ans即可。
 
#include 
#define inf 0x3f3f3f3f#define met(a,b) memset(a,b,sizeof a)#define pb push_back#define mp make_pair#define inf 0x3f3f3f3fusing namespace std;typedef long long ll;const int N = 2e5+50;const int mod = 1e9+7;const double pi= acos(-1.0);typedef pair
pii;int n,k;int a[N];int mn[N][20],mx[N][20],mm[N];void init() { for(int j=1; j<=mm[n]; ++j) { for(int i=1; i+(1<
<=n; ++i) { mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]); mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]); } }}int getmx(int l,int r) { int k = mm[r-l+1]; return max(mx[l][k],mx[r-(1<
k)l=mid+1; else r=mid-1,res=mid; } ans+=i-res+1; } printf("%lld\n",ans); return 0;}

 

转载于:https://www.cnblogs.com/jianrenfang/p/7257227.html

你可能感兴趣的文章
MAC下安装与配置MySQL
查看>>
linux系统的crond服务
查看>>
Restore Volume 操作 - 每天5分钟玩转 OpenStack(60)
查看>>
sqool导出oracle数据
查看>>
MyBatis动态传入表名,字段名参数的解决办法
查看>>
Windows平台下安装Hadoop
查看>>
oracle11gR2静默安装
查看>>
理解javascript中的浏览器窗口——窗口基本操作
查看>>
Directx11学习笔记【二十】 使用DirectX Tool Kit加载mesh
查看>>
【Linux】NAT模式下关于主机ping不通虚拟机的问题
查看>>
SpringMVC 参数注入
查看>>
mysql去重, 把url重复且区为空的中去掉、统计重复数据、、结果集去重合并成一行...
查看>>
atitit.attilax的软件 架构 理念.docx
查看>>
EF实体框架之CodeFirst四
查看>>
[Tex学习]WinEdit 常用软件快捷键
查看>>
二维码在短信业务应用的初步构思
查看>>
分布式服务器集群架构方案思考
查看>>
Graphviz使用简介(中文乱码的问题)
查看>>
Log4J使用
查看>>
【反编译系列】三、反编译神器(jadx)
查看>>