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 p1p2,..., pn andthe third line consists of n integers s1s2,..., sn,separated by single spaces. The numbers pi and sisatisfy 0pi < si1000 and denote thefloor and ceiling level at interval [ii + 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;

}

 

0 0
原创粉丝点击