There is a map of the cave, to store water in this cave, requires water can not touch the top of the cave. Now give the top position and ground height of each position. How much water can I put up?
Problem-solving ideas: According to the physical theorem, each section has a continuous interval of water, the water level must be equal
So we can find the height of the water level in the same continuous interval, the height of which is equal to the height of the lowest cave top. Based on this, from left to right update, and then from right to left to update, you can get the height of each position of the water level
#include <cstdio>#include <algorithm>using namespace STD;Const intN =1000010;Const intINF =0x3f3f3f3f;intS[n], p[n], N;voidInit () {scanf("%d", &n); for(inti =0; I < n; i++) {scanf("%d", &p[i]); } for(inti =0; I < n; i++) {scanf("%d", &s[i]); }}intSolve () {intt = INF; for(inti =0; I < n; i++) {t = min (t, s[i]); t = max (T, P[i]); S[i] = t; } t = INF; for(inti = n-1; I >=0; i--) {t = min (t, s[i]); t = max (T, P[i]); S[i] = t; }intAns =0; for(inti =0; I < n; i++) ans + = s[i]-p[i];returnAns;}intMain () {intTestscanf("%d", &test); while(test--) {init ();printf("%d\n", solve ()); }return 0;}
UVALive-4621 Cav greedy + analysis