1442 - Cav
来源:互联网 发布:人工智能 招聘 深圳 编辑:程序博客网 时间:2024/05/21 06:59
As an owner of a land with a cave you weredelighted 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, theefflective volume is not easy to calculate, because the cave has a rathersophisticated shape (see figure). Thank heavens it is degenerate in onedimension!
The cave. All ponds that can be floodedwith fuel are marked black.
Furthermore, there is some electricalwiring on the ceiling of the cave. You can never be sure if the insulation isintact, so you want to keep the fuel level just below the ceiling at everypoint. You can pump the fuel to whatever spots in the cave you choose, possiblycreating several ponds. Bear in mind though that the fuel is a liquid, so itminimises its gravitational energy, e.g., it will run evenly in every directionon a flat horizontal surface, pour down whenever possible, obey the rule ofcommunicating vessels, etc. As the cave is degenerate and you can make thespace between the fuel level and the ceiling arbitrarily small, you actuallywant to calculate the maximum possible area of ponds that satisfyaforementioned rules.
Input
The input contains several test cases. Thefirst line of the input contains a positive integerZ15, denoting the number oftest cases. Then Z test cases follow, each conforming to theformat described below
In the first line of an input instance, there is an integer n (1n106) denotingthe width of the cave. The second line of input consists of n integers p1, p2,..., pn andthe third line consists of n integers s1, s2,..., sn,separated by single spaces. The numbers pi and sisatisfy 0pi < si1000 and denote thefloor and ceiling level at interval [i, i + 1),respectively.
Output
For each test case, your program has towrite an output conforming to the format described below.
Your program is to print out one integer: the maximum total area of admissibleponds in the cave.
SampleInput
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
SampleOutput
14
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1000000 + 5;
int n, p[maxn], s[maxn], h[maxn];
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", &p[i]);
for(int i = 0; i < n; i++) scanf("%d", &s[i]);
int ans = 0, level = s[0];
for(int i = 0; i < n; i++) {
if(p[i] > level) level = p[i];
if(s[i] < level) level = s[i];
h[i] = level;
}
level = s[n-1];
for(int i = n-1; i >= 0; i--) {
if(p[i] > level) level = p[i];
if(s[i] < level) level = s[i];
ans += min(h[i], level) - p[i];
}
printf("%d\n", ans);
}
return 0;
}
- 1442 - Cav
- 1442 - Cav
- 1442 - Cav(扫描+推理)
- UVA - 1442 Cav
- UVa OJ 1442 - Cav
- UVA-1442 Cav
- Cav
- uva 1442 - Cav(贪心)
- uva - 1442(cav 递推类 ,思路)
- UVa 1442:Cav(杂题)
- uva 1442 Cav(左右各扫描一次)
- LA4621 Cav
- Cav UVA
- Cav UVA
- UVALive - 4621 Cav
- CERC 2009 Cav 扫描法 (uvaLive 4621 - Cav)
- UVALive - 4621 Cav 贪心 + 分析
- 1st Air Cav Shifts Tactics, Enables Iraqis to Complete Mission
- leetcode:Valid Palindrome 菜鸟解法
- 120 - Stacks of Flapjacks
- hibernate:java.math.BigInteger cannot be cast to java.lang.Long
- 714 - Copying Books
- 1152 - 4 Values whose Sum is 0
- 1442 - Cav
- 1451 - Average
- 网站加速--服务器编写篇(转)
- 计算器的实现——基础篇(一)
- jetty嵌入式 webApp开发(Simple Demo)
- 1471 - Defense Lines
- 1605 - Building for UN
- 1606 - Amphiphilic Carbon Molecules
- 1607 - Gates