UVA1442-Cav(扫描法)
Accept: 185 Submit: 679
Time Limit: 3000 mSec
Problem Description

Input
The input contains several test cases. The first line of the input contains a positive integer Z ≤ 15, denoting the number of test cases. Then Z test cases follow, each conforming to the format described below. In the first line of an input instance, there is an integer n (1 ≤ n ≤ 106) denoting the width of the cave. The second line of input consists of n integers p1,p2,...,pn and the third line consists of n integers s1,s2,...,sn, separated by single spaces. The numbers pi and si satisfy 0 ≤ pi < si ≤ 1000 and denote the floor and ceiling level at interval [i,i + 1), respectively.
Output
Sample Input
15
6 6 7 5 5 5 5 5 5 1 1 3 3 2 2
10 10 10 11 6 8 7 10 10 7 6 4 7 11 11
Sample Output
14
题解:扫描法。对于每一个单位小区间[i,i+1],它的最高高度应该满足,在这个高度向左右两边发出两条射线,这两条射线应穿出洞穴或者碰到下面的墙壁,这样一来,我们就可以用扫描法从左到右和从右到左分别进行扫描,从左到右扫描时只看左边的射线,右边同理。扫描的过程,其实就是在维护一个最高高度,由物理的连通器原理,当前单位小区间的高度不应高于左边的小区间,但是如果该区间和左边不连通,那就可以大于左边的高度,这种情况也就是该处墙壁最低点高于左边水位,如果当前高度大于上面的墙壁,那么肯定要降低到上面墙壁的高度,利用这样两条规则就可以扫描出合理的最高高度。
#include <bits/stdc++.h> using namespace std; const int maxn = + ; int n;
int low[maxn], high[maxn];
int h[maxn]; int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d", &n);
for (int i = ; i <= n; i++) {
scanf("%d", &low[i]);
}
for (int j = ; j <= n; j++) {
scanf("%d", &high[j]);
} int level = high[];
h[] = level;
for (int i = ; i <= n; i++) {
if (level > high[i]) level = high[i];
if (level < low[i]) level = low[i];
h[i] = level;
} h[n] = min(h[n], high[n]);
level = h[n];
for (int i = n - ; i >= ; i--) {
if (level > high[i]) level = high[i];
if (level < low[i]) level = low[i];
h[i] = min(h[i], level);
} int ans = ; for (int i = ; i <= n; i++) {
ans += h[i] - low[i];
} printf("%d\n", ans);
}
return ;
}
UVA1442-Cav(扫描法)的更多相关文章
- uva1442 Cav
连通器向左向右扫描两次即可每一段有水的连通区域,高度必须相同,且不超过最低天花板高度if(p[i] > level) level = p[i]; 被隔断,要上升(隔断后,之前的就不变了,之后的从 ...
- 【BZOJ-1670】Building the Moat护城河的挖掘 Graham扫描法 + 凸包
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 464 Solv ...
- 计算几何 : 凸包学习笔记 --- Graham 扫描法
凸包 (只针对二维平面内的凸包) 一.定义 简单的说,在一个二维平面内有n个点的集合S,现在要你选择一个点集C,C中的点构成一个凸多边形G,使得S集合的所有点要么在G内,要么在G上,并且保证这个凸多边 ...
- 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)
[题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...
- Graham 扫描法找凸包(convexHull)
凸包定义 通俗的话来解释凸包:给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边型,它能包含点集中所有的点  Graham扫描法 由最底的一点 \(p_1\) 开始(如果有多个这样的点, ...
- 关于graham扫描法求凸包的小记
1.首先,凸包是啥: 若是在二维平面上,则一般的,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边型,它能包含点集中所有的点. ───────────────────────────── ...
- Graham扫描法
Graham扫描法求凸包的模板 运行之后可以得到存有凸包顶点的栈s和栈顶指针top,n代表总点数 这个模板我当时调了很久,主要难点有两个,一个是正确的极角排序,一个是出栈入栈的细节操作,逆时针扫描,这 ...
- HTML5QQ登录cav demo
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="tex ...
- 凸包算法(Graham扫描法)详解
先说下基础知识,不然不好理解后面的东西 两向量的X乘p1(x1,y1),p2(x2,y2) p1Xp2如果小于零则说明 p1在p2的逆时针方向 如果大于零则说明 p1在p2的顺时针方向 struct ...
随机推荐
- The JRE_HOME environment variable is not defined correctly This environment
昨天启动tomcat还好好的,今天不知道抽什么风,cmd中报错: The JRE_HOME environment variable is not defined correctly This env ...
- 详解margin: auto
auto是margin的可选值之一.相信大家平时使用auto值时,最多的用法大概是 margin: 0 auto; 和 margin: auto; 不过你可能也发现了不论是 margin: auto; ...
- LeNet训练MNIST
jupyter notebook: https://github.com/Penn000/NN/blob/master/notebook/LeNet/LeNet.ipynb LeNet训练MNIST ...
- mapper代理开发步骤
1:先写Mapper接口,UserMapper.java 2:然后遵循4条开发规范,写映射文件,UserMapper.xml 3:将映射文件,UserMapper.xml加入到SqlMapConfig ...
- Exception 和 Error 有什么区别么
声明 本篇所涉及的提问,正文的知识点,全都来自于杨晓峰的<Java核心技术36讲>,当然,我并不会全文照搬过来,毕竟这是付费的课程,应该会涉及到侵权之类的问题. 所以,本篇正文中的知识点, ...
- bootstrap日期控件(双日期、清空等问题解决)
bootstrap以它优美的外观和丰富的组件,使它成为目前最流行的前端框架.在项目开发中,我们使用它的日期控件确实遇到了一些问题: 1.日期控件后面两个图标点击触发失效 2.双日期关联问题 3.双日期 ...
- 显示Linux进程打开了哪些文件? 用lsof命令
1.lsof abc.txt --> 显示开启文件 abc.txt的进程. 2.lsof -c abc ---> 显示abc进程现在打开的文件. 3.lsof -c -p 1234 -- ...
- python特性--property
在定义一个类的时候,有时我们需要获取一个类的属性值,而这个属性值需要经过类中的其他属性运算来获得的.那么很容易,只要我们在类中定义一个方法,并且通过调用方法可以获取到那个需要运算的属性值.那么,问题来 ...
- Centos 7 查看内存占用情况相关命令
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80652626 本文出自[赵彦军的博客] 1. top命令 top命令经常用来监控l ...
- git 入门教程之忽略文件
忽略文件 "并不是所有的牛奶都叫特仑苏",在版本控制系统中也有相似的表达,那就是"并不是所有的文件都需要提交". 有的是因为没必要提交,比如日志文件,系统缓存文 ...