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. python同步、互斥锁、死锁

    目录 同步 同步的概念 解决线程同时修改全局变量的方式 互斥锁 使用互斥锁完成2个线程对同一个全局变量各加9999999 次的操作 上锁解锁过程 总结 死锁 避免死锁 同步 同步的概念 同步就是协同步 ...

  2. Python 常用系统模块整理

    Python中的常用的系统模块中部分函数等的整理 random: 随机数 sys: 系统相关 os: 系统相关的 subprocess: 执行新的进程 multiprocessing: 进程相关 th ...

  3. 网页字体在Frontpage2000制作网页中的讲解

    运用HTML,我们可以对字体的大小及字形进行简单的修改,但要进行统一地控制.创建特殊效果,就必须要用到CSS.它能让您更有效地控制网页外观,并可以扩充精确指定网页元素位置,外观以及创建特殊效果的能力. ...

  4. iphone手机投屏在哪里 手机无线投屏电脑

    Iphone是我们经常使用的一款手机,有时候经常需要将一些文件图片信息等投屏到电脑,那么iphone手机投屏在哪里?可以无线投屏到电脑吗?其实很简单,下面就分享下苹果手机投屏的具体方法给大家,希望对大 ...

  5. python语言学习---3

    第四天 1.set 持有一系列元素,这一点和 list 很像,但是set的元素没有重复,而且是无序的, 这点和 dict 的 key很像. (不信可以输出下试试 ~-~ )另外,其存储的对象必须不可变 ...

  6. (转)Android 之生成图形验证码

    import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; impor ...

  7. Python 套接字socketserver网络编程

    为什么使用socketserver 虽然Python内置的socket和threading模块能实现简单的多线程服务器,在非正式环境,随便用用还是可以的,但是如果要在生产环境中使用,那是万万不够的. ...

  8. Visual Studio语言设置

    按照的是中文的visual studio,用起来很不方便,因为程序员的都是英文版,平时交流时也是英文的名字 转换语言时发现只有中文和跟随windows系统的设置 官方给的文档看的不是很清楚 查阅资料后 ...

  9. SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图文解决方法

    SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图文解决方法 SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图 ...

  10. mssql sqlserver 规范使用方法分享

    转自:http://www.maomao365.com/?p=5586 摘要:下文主要讲述sql server表设计及脚本编写中,相关规范 ———————————数据表字段类型选择:字符类型根据长度选 ...