博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
51nod 1215 数组的宽度
阅读量:4609 次
发布时间:2019-06-09

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

若一个数在一段区间内作为最大值存在,那么答案应该加上这个数

若一个数在一段区间内作为最小值存在,那么答案应该减去这个数

所以我们利用单调栈,高效求出a[i]在哪个区间内作为最大/最小值存在,从而确定,加/减多少次a[i],最终得到答案

#include
#include
#include
#include
#include
#include
#include
#define MAXSIZE 100005#define LL long longusing namespace std;const LL INF=1e19;LL a[MAXSIZE];int s[MAXSIZE];int main(){ int n,top; LL ans = 0; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); a[n+1] = 0; top = 0; for(int i=1;i<=n+1;i++) { if(top == 0) { s[++top] = i; } else { while(top>=1 && a[s[top]] > a[i]) { ans -= (i-s[top])*(s[top]-s[top-1])*a[s[top]]; top--; } s[++top] = i; } } top = 0; a[n+1] = INF; for(int i=1;i<=n+1;i++) { if(top == 0) { s[++top] = i; } else { while(top>=1 && a[s[top]] < a[i]) { ans += (i-s[top])*(s[top]-s[top-1])*a[s[top]]; top--; } s[++top] = i; } } printf("%lld\n",ans); return 0;}
View Code

 

转载于:https://www.cnblogs.com/alan-W/p/8385053.html

你可能感兴趣的文章
PIE SDK矢量数据的投影转换
查看>>
POJ 1636
查看>>
viewport
查看>>
编译LNMP部署动态网站环境
查看>>
为什么给GIT库打TAG不成功
查看>>
android-数据持久化存储之Content Provider
查看>>
【算法】背包问题初探
查看>>
Zigbee组网原理详解
查看>>
spring boot + druid + 封装JdbcTemplate
查看>>
OpenCV GUI基本操作,回调函数,进度条,裁剪图像等
查看>>
SQLCODE和SQLERRM .
查看>>
sql - sum() 和 count() 函数的区别
查看>>
linux mysql 安装(rpm)
查看>>
css类选择器类名覆盖优先级
查看>>
Linux常见命令
查看>>
函数的定义
查看>>
guess
查看>>
bootstrap以及考试复习
查看>>
android 中检查设备是否有网络可用
查看>>
linux磁盘命令-lsblk显现磁盘阵列分组
查看>>