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 ...
随机推荐
- c3p0链接池配置使用
c3p0链接池初步使用:直接上代码 c3p0是开源面粉的连接池,目前使用它的开源项目主要有:Spring,Hibernate等,使用时需要导入相关jar包及配置文件c3p0-config.xml文件 ...
- es6 语法 (map、set和obj 的对比)
//数据结构对比 增查改删 { //map.set和Object let item = {t:1}; let map = new Map(); let set = new Set(); let obj ...
- Foundry feats. MultiverseStudio
https://www.foundry.com/news-awards/foundry-jcube-announcement 经过这么多年的过程,本周本产品终于发布了PR,这次是由Foundry独家代 ...
- listview 异步加载图片并防止错位
1.图片错位原理: 如果我们只是简单显示list中数据,而没用convertview的复用机制和异步操作,就不会产生图片错位:重用convertview但没用异步,也不会有错位现象.但我们的项目中li ...
- MFC 单文档添加按钮
VS 单文档 添加按钮 今天做项目需要在单文档中创建按钮来响应函数,即点击按钮,调用某函数,特此记录. 1. 在XXXView中添加CButton变量,例如 CButton start; 2. 下来就 ...
- 章节三、5-Getters-Setters和this关键字part01
我们新建第一个Car类: package introduction; public class Car { //颜色 private String color; //生成商 private Strin ...
- JS 文本框格式化
页面: <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> & ...
- Microsoft SQL Server 17导出xlsx文件时报错:The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. (System.Data)
导出数据时报错: 如果你是导出office 2007格式 TITLE: SQL Server Import and Export Wizard ---------------------------- ...
- bootstrap-paginator分页示例 源码 MVC
准备 1.数据:bootstrap包(含分页插件bootstrap-paginator.js) 2.技术方案:ajax动态加载分页.部分视图.BLL取数 代码 模板页 @{ Layout = null ...
- MapReduce ----数据去重
三个文件 2017-03-10 a2017-03-11 b2017-03-12 d2017-03-13 d2017-03-142017-03-15 a 2017-03-10 e2017-03-11 b ...