Problem E: 积木积水

Description

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

Input

第一行包含一个整数T(T≤100),表示接下来的测试样例个数。 每个测试样例有两行组成: 第一行包含一个整数N(N≤1e6),表示积木的列数; 第二行包含N个整数Ai(Ai≤1e6),表示第i列积木的个数。

Output

每个样例输出一行,包含一个整数,为题目所求。

Sample Input

1
11
6 2 2 4 2 0 3 4 4 5 1

Sample Output

19
 
解题思路:首先找到最高的那个位置,然后向左右找第二高的位置,把这中间的积水算出来,一直往左右去找。
#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: 积木积水 ——————【模拟】的更多相关文章

  1. 广东工业大学2016校赛决赛重现——E积木积水(方法据说很多)

    Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...

  2. A+B and A*B problem 大数相加 相乘 模拟

    A+B and A*B problem 大数相加 相乘 模拟 题意 给你两个数a和b,这两个数很大,然后输出这两个数相加的和,相乘的积. 解题思路 模拟,但是还是搜了搜代码实现,发现这个大佬写的是真的 ...

  3. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  4. UVa 101 - The Blocks Problem(积木问题,指令操作)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  5. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem D: LC-Display(模拟计算器显示数字)

    Problem D: LC-Display Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 14  Solved: 3[Submit][Status][We ...

  6. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem B: Minesweeper(模拟扫雷)

    Problem B: Minesweeper Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 29  Solved: 7[Submit][Status][W ...

  7. uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟

    挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. js Array操作

    JS中数组的操作 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长 ...

  2. rtabmap and rtabmap_ros make error(rtabmap编译错误)

    Build from source following README.nd in rtabmap_ros rtabmap make error Error 1 make[2]: *** No rule ...

  3. Atcoder Beginner Contest 121D(异或公式)

    #include<bits/stdc++.h>using namespace std;int main(){    long long a,b;    cin>>a>&g ...

  4. List_insert

    List_insert /* Sorting from little to large use List */ #include <stdio.h> /* printf, scanf, N ...

  5. Spring Boot的每个模块包详解

    Spring Boot的每个模块包详解,具体如下: 1.spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. 2.spring-boot-s ...

  6. 关于函数传参的其他问题(const形参实参/可变形参)

    const 形参和实参 当形参是 const 变量时,实参是 const 或者不是 const 变量都可以. 实参初始化形参时会忽略掉顶层 const: void gel(const int a){ ...

  7. Django 学习:为窗体加上防机器人的验证机制(验证码功能)

    这里我们使用 django-simple-captcha 模块,官方介绍如下:https://github.com/mbi/django-simple-captcha 一键安装: pip instal ...

  8. FJWC2019 子图 (三元环计数、四元环计数)

    给定 n 个点和 m 条边的一张图和一个值 k ,求图中边数为 k 的联通子图个数 mod 1e9+7. \(n \le 10^5, m \le 2 \times 10^5, 1 \le k \le ...

  9. DataRow[]转DataTable

    DataRow[]有个扩展方法CopyToDataTable()

  10. gnome-terminal

    在终端中打开终端: gnome-terminal 同时打开多个终端: gnome-terminal --window --window 此处有几个 --window 就会打开几个终端 最大化形式打开终 ...