Problem E: 积木积水 ——————【模拟】
Problem E: 积木积水
Description
现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水。小明又是如此地喜欢二次元,于是他把这个三维的现实问题简化成二维的问题。设雨量无穷、积木不透水、积木间无缝连接,问在这个二次元的世界里,已放置好的积木会有多少单位的积水量?

Input
第一行包含一个整数T(T≤100),表示接下来的测试样例个数。 每个测试样例有两行组成: 第一行包含一个整数N(N≤1e6),表示积木的列数; 第二行包含N个整数Ai(Ai≤1e6),表示第i列积木的个数。
Output
每个样例输出一行,包含一个整数,为题目所求。
Sample Input
Sample Output
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 1e6+200;
typedef long long LL;
int a[maxn], bef[maxn], beh[maxn];
int main(){
int cas, n;
scanf("%d",&cas);
while(cas--){
memset(bef,0,sizeof(bef));
memset(beh,0,sizeof(beh));
scanf("%d",&n);
int Maxh = 0, Maxid = 1;
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
if(bef[i-1] < a[i]){
bef[i] = a[i];
}else{
bef[i] = bef[i-1];
}
if(a[i]>Maxh){
Maxid = i;
Maxh = a[i];
}
}
int l = 1, r = n;
for(int i = 2;i <= n; i++){
if(a[i] <= a[i-1]){
break;
}else{
l = i;
}
}
for(int i = n-1;i >= 1; i--){
if(a[i] <= a[i+1]){
break;
}else{
r = i;
}
}
if(l >= r){
puts("0"); continue;
}
for(int i = n; i >= Maxid; i--){
if(beh[i+1] > a[i]){
beh[i] = beh[i+1];
}else{
beh[i] = a[i];
}
}
LL sum = 0, tmp = 0;
int c = 0;
int hei = Maxh;
beh[r+1] = -1;
for(int i = Maxid+1; i <= r; i++){
if(beh[i]!=beh[i+1]){
sum += tmp - (c*(hei-a[i]));
hei = a[i];
tmp = 0;
c = 0;
}else{
tmp += hei - a[i];
c++;
}
}
hei = Maxh; tmp = 0; c = 0;
bef[l-1] = -1;
for(int i = Maxid-1; i >= l; i--){
if(bef[i]!=bef[i-1]){
sum += tmp - c*(hei-a[i]);
hei = a[i];
tmp = 0; c = 0;
}else{
tmp += hei - a[i];
c++;
}
}
printf("%lld\n",sum);
}
return 0;
}
Problem E: 积木积水 ——————【模拟】的更多相关文章
- 广东工业大学2016校赛决赛重现——E积木积水(方法据说很多)
Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...
- A+B and A*B problem 大数相加 相乘 模拟
A+B and A*B problem 大数相加 相乘 模拟 题意 给你两个数a和b,这两个数很大,然后输出这两个数相加的和,相乘的积. 解题思路 模拟,但是还是搜了搜代码实现,发现这个大佬写的是真的 ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- UVa 101 - The Blocks Problem(积木问题,指令操作)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem D: LC-Display(模拟计算器显示数字)
Problem D: LC-Display Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 14 Solved: 3[Submit][Status][We ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem B: Minesweeper(模拟扫雷)
Problem B: Minesweeper Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 29 Solved: 7[Submit][Status][W ...
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem I. Interest Targeting 模拟题
Problem I. Interest Targeting 题目连接: http://codeforces.com/gym/100714 Description A unique display ad ...
随机推荐
- C# 判断一个数是不是奇数/偶数
一般普通版: private bool IsOdd(int num) { ) == ; } 通过判断取余 现在升级版: private bool IsOdd(int num) { ) == ; } 通 ...
- 【转】WinForm时间选择控件(DateTimePicker)如何选择(显示)时分秒
源地址:https://www.cnblogs.com/EvanFan/p/7826160.html 注意:年月日时分秒的大小写格式,如果错了数据就错了,如果能显示当前时间说明设置正确
- Python实例手册
在电脑中突然发现一个这么好的资料,雪松大神制作,不敢独享,特与大家共享.连他的广告也一并复制了吧! python实例手册 #encoding:utf8 # 设定编码-支持中文 0说明 手册制作: 雪松 ...
- Javascript 上传文件到Azure存储
对一些前端工程师来讲,使用javascript上传文件到Azure存储中可能是需要掌握的技能,希望这篇博客能给到帮助. 在开始前我们需要了解以下几点: 共享访问签名(Shared Access Sig ...
- ubuntu 上的python不能解析jpeg,png?
在安装pillow之前需要先安装以下支持包1.apt-get install libjpeg-dev libfreetype6-dev zlib1g-dev libpng12-dev2.安装pillo ...
- 20165224 陆艺杰 《Java程序设计》课程总结
每周作业链接汇总 https://www.cnblogs.com/lyj-/p/8414278.html https://www.cnblogs.com/lyj-/p/8695018.html htt ...
- CodeForces 1043D Mysterious Crime 区间合并
题目传送门 题目大意: 给出m个1-n的全排列,问这m个全排列中有几个公共子串. 思路: 首先单个的数字先计算到答案中,有n个. 然后考虑多个数字,如果有两个数字相邻,那么在m个串中必定都能找到这两个 ...
- 使用JDBC连接了数据库的图书管理系统2.0
更新日志: 2019.3.28 数据库版本2.0 1.使用mySQL数据库 2.修改代码使用JDBC连接数据库 3.新增Manage操作类及DBUtils数据库工具类 4.完善代码(封装及方法调用) ...
- 什么是SocLib
一.SocLib简介 SoCLib是用于多处理器片上系统(MP-SoC)虚拟原型开发的开放平台.该项目始于ANR创建的项目, 现在维持在https://www.lip6.fr/.该平台的核心是用于虚拟 ...
- 小问题总结:鼠标点击到输入框(input)里的时候,输入框的提示消失,鼠标再移开,输入框提示出现
问题如标题: 鼠标点击到输入框(input)里的时候,输入框的提示消失,鼠标再移开,输入框提示出现.如图所示: 做法如下: <input type="text" name ...