Problem E: Faucet FlowA faucet is pouring water into a long, thin aquarium with varous vertical dividers (Wils) in it. the aquarium is initially empty, and its bottom is perfectly level. how long will it take for water to spill over its left-or right-most divider? The faucet is abve location x = 0, and the dividers are located at x =-1,-3,-5 ,..., leftx and 1, 3, 5 ,..., rightx. the dividers are attached perpendicular to the floor and sides of the aquarium, and have various heights. the aquarium's length is greater than rightx-leftx, its Wils are higher than the highest divider, and its width is 1 unit everywhere. water pours from the faucet at a rate of 1 cubic unit per second. [You may assume that water is an ideal liquid: it always flows downhill and if it cannot flow downhill it spreads at an equal rate in all horizontal directions ctions.] inputEach test case consists of two integers leftx (an odd number <=-1) and rightx (an odd number> = 1 ). subsequent lines contain the height (a positive integer) of each divider from left to right. there will be no more than 1000 dividers in any test case. input is terminated with a line containing two zeros. outputFor each case, output an integer on a single line, indicating how long it will take, in seconds, before water starts spilling over either the left or right divider. sample Input
-1 13 5-3 34 3 2 1-3 51 2 2 1 10 0
Sample Output
668
Question: Given the height of some frequency divider, start with [-] And ask when to scatter.
Idea: disgusting details, greedy, find the largest Separator on the left and the highest on the right. If they are the same, the time is the middle time plus the less time on both sides. If one side is relatively large, it will certainly eventually flow to the other side, and the time is calculated to be small, but pay attention to the amount of water that may flow to the other side, because if there are equal separators on both sides, if the water flow is halved, it can also be converted into a flow of more water to the other side, so that you can add this part of time.
Code:
#include
#include
#define min(a,b) ((a)<(b))?(a):(b)const int N = 2005;int l, r, f[N], lv, rv, lmax, rmax, O;void init() {O = -l;for (int i = l; i <= r; i += 2)scanf("%d", &f[i + O]);lv = -1, rv = 1, lmax = f[lv + O], rmax = f[rv + O];}int solve() {int ans = 0, i, lt = 0, rt = 0;for (i = -1; i >= l; i -= 2) {if (f[i + O] > lmax) {lmax = f[i + O];lv = i;}}for (i = 1; i <= r; i += 2) {if (f[i + O] > rmax) {rmax = f[i + O];rv = i;}}int ll = l;for (i = l; i <= lv; i += 2) {if (f[i + O] >= f[ll + O]) {lt += (i - ll) * f[ll + O];ll = i;}}int rr = r;for (i = r; i >= rv; i -= 2) {if (f[i + O] >= f[rr + O]) {rt += (rr - i) * f[rr + O];rr = i;}}if (lmax == rmax)ans = (rv - lv) * lmax + (min(lt, rt)) * 2;else {if (lmax > rmax) {for (i = -1; i >= l; i -= 2)if (f[i + O] >= rmax) {lmax = f[i + O]; lv = i;break;}if (lmax == rmax) {int lvv = lv;for (i = lv; i >= l; i -= 2) {if (f[i + O] > rmax) {lmax = f[i + O]; lvv = i;break;}}ans = (rv - lv) * rmax + (min((lv - lvv) * rmax, rt)) + rt;}else ans = rmax * (rv - lv) + rt;}else {for (i = 1; i <= r; i += 2)if (f[i + O] >= lmax) {rmax = f[i + O]; rv = i;break;}if (lmax == rmax) {int rvv = rv;for (i = rv; i <= r; i += 2) {if (f[i + O] > lmax) {rmax = f[i + O]; rvv = i;break;}}ans = (rv - lv) * lmax + (min((rvv - rv) * lmax, lt)) + lt;}else ans = lmax * (rv - lv) + lt;}}return ans;}int main() {while (~scanf("%d%d", &l, &r) && l || r) {init();printf("%d\n", solve());}return 0;}