题解:AT_abc359_e [ABC359E] Water Tank
背景
中考结束了,但是暑假只有一天,这就是我现在能在机房里面写题解的原因……
分析
这道题就是个单调栈。
题目上问你第一滴水流到每个位置的时间。我们考虑,答案其实就是比当前木板高且距离当前木板最近的那一个位置的答案加上当前木板的高度与那一个位置的距离构成的矩形面积再减去中间较低木板构成的矩形面积。这个思路联想到单调栈的话还是很好想到的。
所以我们存一个结构体栈,两个参数分别表示这个位置木板的高度和与上一个比它高的木板的距离。
在更新的时候,先按照一般单调栈操作用当前木板高度去更新栈的信息,用一个变量统计距离之和,并把每个栈位最终会影响答案的矩形面积累加一下。放一下代码:
while(!st.empty()&&st.top().h<=h[i])
{
int now=st.top().h,sum=st.top().sum;
dat+=now*sum;
t+=sum;
st.pop();
}
然后按照思路更新当前答案并输出,再让当前木板高度和距离进栈即可。
Code
#include<bits/stdc++.h>
#define int long long
#define mod 998244353
using namespace std;
inline int read()
{
int w=1,s=0;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
while(isdigit(ch)){s=s*10+(ch-'0');ch=getchar();}
return w*s;
}
const int maxn=1e6+10;
int n,h[maxn];
int ans=1;
struct no
{
int h,sum;
};
stack<no> st;
signed main()
{
// freopen("xxx.in","r",stdin);
// freopen("xxx.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++)h[i]=read();
for(int i=1;i<=n;i++)
{
int t=1,dat=0;
while(!st.empty()&&st.top().h<=h[i])
{
int now=st.top().h,sum=st.top().sum;
dat+=now*sum;
t+=sum;
st.pop();
}
ans=ans+h[i]*t-dat;
st.push({h[i],t});
printf("%lld ",ans);
}
return 0;
}
题解:AT_abc359_e [ABC359E] Water Tank的更多相关文章
- HDU 5575 Discover Water Tank(左偏树)
https://vjudge.net/problem/HDU-5575 题意: 有一个水箱,被n-1块板子分成了n个部分,板子的高度不尽相同.现在有m次探测,每次探测在第x部分的y+0.5高度处是否有 ...
- Leetcode 题解 Trapping Rain Water
题目链接:https://leetcode.com/problems/trapping-rain-water/description/ 思路: 1.先找到最左侧第一个高度不为0的柱子i. 2.从i+1 ...
- HDU 5575 Discover Water Tank 并查集 树形DP
题意: 有一个水槽,边界的两块板是无穷高的,中间有n-1块隔板(有高度),现有一些条件(i,y,k),表示从左到右数的第i列中,在高度为(y+0.5)的地方是否有水(有水:k = 1),问最多能同时满 ...
- hdu5575 Discover Water Tank
题意: 给出个水箱,水箱两侧有无限高的隔板,水箱内有整数高度的隔板将水箱分成n-1份,现在给出m个限制,每个限制表示某个位置的某个高度有水或没水,问最多能同时满足多少个限制.n,m<=2*10^ ...
- CF414D Mashmokh and Water Tanks
CF414D Mashmokh and Water Tanks 洛谷评测传送门 题目描述 Mashmokh is playing a new game. In the beginning he has ...
- PID控制器(比例-积分-微分控制器)- I
形象解释PID算法 小明接到这样一个任务: 有一个水缸点漏水(而且漏水的速度还不一定固定不变),要求水面高度维持在某个位置,一旦发现水面高度低于要求位置,就要往水缸里加水. 小明接到任务后就一直守在水 ...
- ZOJ 5579 Stean
Stean Time Limit: 1 Second Memory Limit: 65536 KB Special Judge Tom is good at making stea ...
- CodeForces 362E Petya and Pipes
Petya and Pipes Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- What are CBR, VBV and CPB?
转自:https://codesequoia.wordpress.com/2010/04/19/what-are-cbr-vbv-and-cpb/ It's common mistake to to ...
- [LeetCode]题解(python):042-Trapping Rain Water
题目来源 https://leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representing a ...
随机推荐
- Deepin15.11+WIN10 双系统安装过程与遇到的问题(一)
一.deepin安装流程 1.下载 下载深度系统最新版本官网https://www.deepin.org/zh/download/下载深度系统专用U盘启动盘制作工具https://www.deepin ...
- request to https://registry.npm.taobao.org/cnpm failed, reason: certificate has expired
换华为的,否则会出问题:cnpm confg set registry https://mirrors.huaweicloud.com/repository/npm/ npm ERR! code CE ...
- 记第一次用python写界面
花了两三个小时学了Tkinter,做了一个将数据绘制成图的小工具. 1. 获取路径下的所有文件or获取路径下指定名称的文件 1.1 打开文件 //1. 用来放文本框中的文字filename = St ...
- react路由过渡动画效果
render() { return ( <div> <li><Link to="/home">Home</Link></li& ...
- reids分片技术cluster篇
为什么学redis-cluster 前面两篇文章,主从复制和哨兵机制保障了高可用 就读写分离,而言虽然slave节点扩展了主从的读并发能力 但是写能力和存储能力是无法进行扩展,就只能是master节点 ...
- unsupported operand type(s) for +: 'function' and 'str'
unsupported operand type(s) for +: 'function' and 'str' 报错解释:这个错误表明你尝试将一个函数和一个字符串进行加法操作,在Python中,加法不 ...
- git客户端安装和使用
需要安装三个软件 1.git客户端 点击下载 下载完成后一只next就行了. 2.git右键属性的扩展程序 点击下载 下载完成后一只next就行了 3.git中文包 点击下载 下载完成后一只next就 ...
- 结构型模式(Structural Pattern)
模式介绍 结构型模式(Structural Pattern)的主要目的就是将不同的类和对象组合在一起,形成更大或者更复杂的结构体.该模式并不是简单地将这些类或对象摆放在一起,而是要提供它们之间的关联方 ...
- dotnet 融合 Avalonia 和 UNO 框架
现在在 .NET 系列里面,势头比较猛的 UI 框架中,就包括了 Avalonia 和 UNO 框架.本文将告诉大家如何尝试在一个解决方案里面融合 Avalonia 和 UNO 两个框架,即在一个进程 ...
- HBase 中的 JVM 与 GC
HBase中JVM基本配置 在JVM中,默认情况下会设置minimum heap size 为 1/64 可用物理内存,并为maximum heap size设置 1/4 的物理可用内存(不过在Jav ...