【高效算法设计——双向扫描】 UVa 1442 Cave
来源:互联网 发布:搞怪的淘宝收件人名字 编辑:程序博客网 时间:2024/06/07 01:30
Description
As an owner of a land with a cave you were delighted when you last heard that underground fuel tanks are great business. Of course, the more volume one can store, the better. In case of your cave, the efflective volume is not easy to calculate, because the cave has a rather sophisticated shape (see figure). Thank heavens it is degenerate in one dimension!
Furthermore, there is some electrical wiring on the ceiling of the cave. You can never be sure if the insulation is intact, so you want to keep the fuel level just below the ceiling at every point. You can pump the fuel to whatever spots in the cave you choose, possibly creating several ponds. Bear in mind though that the fuel is a liquid, so it minimises its gravitational energy, e.g., it will run evenly in every direction on a flat horizontal surface, pour down whenever possible, obey the rule of communicating vessels, etc. As the cave is degenerate and you can make the space between the fuel level and the ceiling arbitrarily small, you actually want to calculate the maximum possible area of ponds that satisfy aforementioned rules.
Input
The input contains several test cases. The first line of the input contains a positive integer Z15, denoting the number of test cases. Then Ztest cases follow, each conforming to the format described below
In the first line of an input instance, there is an integer n(1n106) denoting the width of the cave. The second line of input consists of nintegers p1, p2,..., pn and the third line consists of n integers s1, s2,..., sn, separated by single spaces. The numbers pi and si satisfy 0pi < si1000 and denote the floor and ceiling level at interval [i, i + 1), respectively.
Output
For each test case, your program has to write an output conforming to the format described below.
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
思路:假设x坐标为i的点,底部高度为s[i],顶部为p[i],那么该点的燃料高度,应该为以它为中心向左右延伸直到碰壁所能达到的最大高度,假设L[i]表示往左能到达的最大高度,R[i]表示往右能达到的最大高度,那么第i点的燃料储备量即min(L[i],R[i])-s[i]
L[i]和R[i]的求法相同,只是扫描的方向相反而已,比如求L[i],我们先把L[0]设为p[0],当前高度为L[i-1]为h,如果h大于p[i],则h为p[i],如果h小与s[i],则h为s[i],代码如下
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>#include<vector>#include<set>#include<map>using namespace std;const int maxn=1000000+5;int s[maxn],p[maxn],level[maxn];int n;inline bool get(int &t){ bool flag = 0 ; char c; while(!isdigit(c = getchar())&&c!='-') if( c == -1 ) break ; if( c == -1 ) return 0 ; if(c=='-') flag = 1 , t = 0 ; else t = c ^ 48; while(isdigit(c = getchar())) t = (t << 1) + (t << 3) + (c ^ 48) ; if(flag) t = -t ; return 1 ;}int main(){ int T,i; get(T); while(T--) { get(n); for(i=0;i<n;i++) get(s[i]); for(i=0;i<n;i++) get(p[i]); int h; h=p[0]; for(i=0;i<n;i++) { if(p[i]<h) h=p[i]; if(s[i]>h) h=s[i]; level[i]=h; } h=p[n-1]; int ans=0; for(i=n-1;i>=0;i--) { if(p[i]<h) h=p[i]; if(s[i]>h) h=s[i]; ans+=min(h,level[i])-s[i]; } printf("%d\n",ans); } return 0;}
- 【高效算法设计——双向扫描】 UVa 1442 Cave
- 【高效算法设计——等价转换】UVa 11054
- 【高效算法设计——二分法】UVa 1607 Gates
- 【高效算法设计——滑动窗口】 UVa 12174 Shuffle
- 【高效算法设计——递归】 UVa 12627 Erratic Expansion
- UVA 1442(p249)----Cave
- 【高效算法设计——扫描法】Amphiphilic Carbon Molecules 极角排序
- 【高效算法设计——滑动窗口】UVa 11572 Unique Snowflakes
- 【高效算法设计——最大值最小问题】UVa 714 Coying Books
- 【高效算法设计——跳跃枚举】Uva 11093 Just Finish it up
- 【高效算法设计——递归】 UVa 1608 Non-boring sequences
- 高效算法设计专项:UVa 10125
- 高效算法设计专项:UVa 10391
- 高效算法设计专项:UVa 10810
- 高效算法设计专项:UVa 10730
- 高效算法设计专项:UVa 10535
- 高效算法设计专项:UVa 11054
- 高效算法设计专项:UVa 11572
- 命题作文:在一棵IPv4地址树中彻底理解IP路由表的各种查找过程
- 黑马程序员-OC语言Block
- PHP + Jquery 实现异步传输验证
- HDU 5186
- 第2周 阅读程序-初识对象(二)
- 【高效算法设计——双向扫描】 UVa 1442 Cave
- hdu 1062 Text Reverse 字符串处理。简单水题
- Rotate Array
- 项目一 用“类”解决旱冰场造价的问题
- LeetCode--Count and Say
- 把书读薄之《面向对象葵花宝典》阅读笔记-面向对象基础
- 微博系统,集成新浪开发,有需要的朋友下载
- auto keyword demo
- WCF 入门教程二