Problem 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

For each test case, your program is to print out one integer: the maximum total area of admissible ponds in the cave.
 

 Sample Input

1
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(扫描法)的更多相关文章

  1. uva1442 Cav

    连通器向左向右扫描两次即可每一段有水的连通区域,高度必须相同,且不超过最低天花板高度if(p[i] > level) level = p[i]; 被隔断,要上升(隔断后,之前的就不变了,之后的从 ...

  2. 【BZOJ-1670】Building the Moat护城河的挖掘 Graham扫描法 + 凸包

    1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 464  Solv ...

  3. 计算几何 : 凸包学习笔记 --- Graham 扫描法

    凸包 (只针对二维平面内的凸包) 一.定义 简单的说,在一个二维平面内有n个点的集合S,现在要你选择一个点集C,C中的点构成一个凸多边形G,使得S集合的所有点要么在G内,要么在G上,并且保证这个凸多边 ...

  4. 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)

    [题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...

  5. Graham 扫描法找凸包(convexHull)

    凸包定义 通俗的话来解释凸包:给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边型,它能包含点集中所有的点  Graham扫描法 由最底的一点 \(p_1\) 开始(如果有多个这样的点, ...

  6. 关于graham扫描法求凸包的小记

    1.首先,凸包是啥: 若是在二维平面上,则一般的,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边型,它能包含点集中所有的点. ───────────────────────────── ...

  7. Graham扫描法

    Graham扫描法求凸包的模板 运行之后可以得到存有凸包顶点的栈s和栈顶指针top,n代表总点数 这个模板我当时调了很久,主要难点有两个,一个是正确的极角排序,一个是出栈入栈的细节操作,逆时针扫描,这 ...

  8. HTML5QQ登录cav demo

    <!DOCTYPE html> <head>    <meta http-equiv="Content-Type" content="tex ...

  9. 凸包算法(Graham扫描法)详解

    先说下基础知识,不然不好理解后面的东西 两向量的X乘p1(x1,y1),p2(x2,y2) p1Xp2如果小于零则说明  p1在p2的逆时针方向 如果大于零则说明 p1在p2的顺时针方向 struct ...

随机推荐

  1. JavaScript事件(含Demo)

    主要事件: onclick 鼠标单击事件 onmouseover 鼠标经过事件 onmouseout 鼠标移开事件 onchange 文本框内容改变事件 onselect 文本框内容被选中事件 onf ...

  2. JavaScript中的三种弹窗

    ---恢复内容开始--- 1.警告窗口(alert) <script type="text/javascript"> alert("我是警告弹框!" ...

  3. 通过css3实现的动画导航菜单代码

    用css3样式实现的滑动导航菜单,html代码如下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" &quo ...

  4. C#字节图片互转、字节转换图片、图片转换字节、byte[]转换图片、图片转换成byte[]

    原文地址:http://www.cnblogs.com/wifi/articles/3522773.html /// <summary> /// 图片转换成字节流 /// </sum ...

  5. jQuery效果之jQuery Color animation 色彩动画扩展

    jQuery 的动画方法(animate)支持各种属性的过渡,但是默认并不支持色彩的过渡,该插件正是来补足这一点! PS: 该插件支持 RGBA 颜色的过渡,但是请注意,IE8以下的版本不支持 RGB ...

  6. jQuery插件之上传文件ajaxfileupload.js源码与使用

    在网页应用中,一般会用到上传文件或者图片什么的到服务器,那么可以用ajaxfileupload.js,但是在使用ajaxfileupload.js时候,当服务器返回的json带有&符号的时候, ...

  7. 【代码笔记】Web-JavaScript-JavaScript 条件语句

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  8. 用ABP只要加人即可马上加快项目进展(二) - 分工篇

    2018年和1998年其中两大区别就是: 前端蓬勃发展, 前后端分离是一个十分大的趋势. 专门的测试人员角色被取消, 多出了一个很重要的角色, 产品经理   ABP只要加入即可马上加快项目进展, 选择 ...

  9. 通过git上传本地代码到github仓库

    最近呢,武汉天气燥热,在公司没啥事,就自己写了一下小demo. 作为一个菜鸟,只在github上扒过别人的代码,还没自己上传过,就试了一下,遇到了一些坑,记录一下. 前提是电脑上安装了git,没有安装 ...

  10. OkHttpHelper使用

    源码:https://gitee.com/xcode_xiao/OkHttpHelper 网络请求缓存的支持,OKHttp Retrofit (get,post,一切,文字,图片,语音,文件,自定义缓 ...