The main idea: a cave with a width of n (n<=10^6) fragments. The ground height in the section I is pi and the top height is si (0=<pi<si<=1000), which requires storing as much fuel (liquid) in the hole as possible, so that the position of the fuel does not touch the top, but can be approached infinitely.
Problem-Solving ideas: Set the fuel height of each fragment is L, the initial value is s[0] or s[n-1], if the current fragment of the ground height pi>l, then l=pi, the top height si<l, then l=si, each fragment of the L exist in the array. Sweep from right to left, then sweep from left to right, and then subtract the smaller l from the fragment, the fuel of the fragment, and all that is to be asked.
#include <cstdio> #include <algorithm>using namespace Std;int N, p[1000010], s[1000010], H[1000010];int main () {int z;scanf ("%d", &z), while (z--) {scanf ("%d", &n), for (int i = 0; i < n; i++) scanf ("%d", &p[i]); for (i NT i = 0; I < n; i++) scanf ("%d", &s[i]), int level = S[0];for (int i = 0; i < n; i++) {if (Level > S[i]) level = S[i];if (Level &L T P[i]) level = p[i];h[i] = level;} Level = S[n-1];int ans = 0;for (int i = n-1; I >= 0; i--) {if (Level > S[i]) level = S[i];if (Level < p[i]) Lev el = P[i];ans + = min (level, h[i])-p[i];} printf ("%d\n", ans);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-1442 Cav