1442 - Cav(扫描+推理)
来源:互联网 发布:mac口红日本买便宜多少 编辑:程序博客网 时间:2024/05/21 14:09
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 Z test 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 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 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
题意:给定一个山洞,长度为n,然后每个位置有顶和底的高度,然后现在在里面存放油,油的最大高度不能没过顶,问最多能存多少油。
思路:既然不能没过顶,那么每个位置最大高度就是顶,那么对应顶的那条横线切过去,遇到比他高的就更新顶,遇到小的就替换用小的,然后从左往右再从右往左更新一遍,记录下每个位置的答案即可。
代码:
#include <stdio.h>#include <string.h>#define INF 0x3f3f3f3fconst int N = 1000005;int t, n;struct Cav {int floor, ceil;}c[N];void init() {scanf("%d", &n);int i;for (i = 0; i < n; i++)scanf("%d", &c[i].floor);for (i = 0; i < n; i++)scanf("%d", &c[i].ceil);}int solve() {int ans = 0, i, ceil = INF;for (i = n - 1; i >= 0; i--) {if (ceil < c[i].floor)ceil = c[i].floor;if (ceil > c[i].ceil)ceil = c[i].ceil;c[i].ceil = ceil;}ceil = INF;for (i = 0; i < n; i++) {if (ceil < c[i].floor)ceil = c[i].floor;if (ceil > c[i].ceil)ceil = c[i].ceil;c[i].ceil = ceil;ans += c[i].ceil - c[i].floor;}return ans;}int main() {scanf("%d", &t);while (t--) {init();printf("%d\n", solve());}return 0;}
- 1442 - Cav(扫描+推理)
- uva 1442 Cav(左右各扫描一次)
- 1442 - Cav
- 1442 - Cav
- CERC 2009 Cav 扫描法 (uvaLive 4621 - Cav)
- UVA - 1442 Cav
- UVa OJ 1442 - Cav
- UVA-1442 Cav
- Cav
- uva 1442 - Cav(贪心)
- uva - 1442(cav 递推类 ,思路)
- UVa 1442:Cav(杂题)
- LA4621 Cav
- Cav UVA
- Cav UVA
- 推理
- UVALive - 4621 Cav
- UVALive - 4621 Cav 贪心 + 分析
- Http报文小探
- 3Sum
- 为什么程序要打包
- 简单字符驱动模板
- 华为3C、酷派大神、红米10号开枪!
- 1442 - Cav(扫描+推理)
- 解决打不开jar包
- Python字典排序的简单实现
- mongodb访问控制及iptables管理
- 并发进程
- 苹果和SONY的反思
- C++容器适配器
- 1:倒排索引
- 全局变量优缺点