Codeforces 460C, codeforces460c
The Code has a detailed explanation, binary simulation is used to find the result, and the greedy choice starts from where to pour the flowers. The principle is that if the flowers need to be watered, the w can use the line segment tree to maintain the line segment, but you can also mark it with a number group. There are always a lot of problems with binary, so writing a lot of output for debugging, jiong
/*************************************** * *********************************> File Name: 460c. cpp> Author: yang> Mail: 826123027@qq.com> Created Time: friday, August 22, 2014, 11:17:43 ************************************* * *********************************/# include <iostream> using namespace std; # include <memory. h> # include <stdio. h> # define N records 5int a [N]; int main () {int n, m, w, memory [N], B [N]; while (cin> n> m> w) {int l = 0x7fffff, r = 0; for (int I = 0; I <n; I ++) {cin> a [I]; if (a [I] <l) l = a [I]; if (a [I]> r) r = a [I];} r + = m; // The longest height while (l <= r) {// No = at the beginning, wa once int tempm = m; int mid = (l + r)> 1; for (int I = 0; I <n; I ++) {B [I] = mid-a [I]; // array B is used to record the number of days required for each flower if (B [I] <= 0) B [I] = 0;} memset (memory, 0, sizeof (memory); // remember when to end w-length watering int index = 0, x = 0; // x indicates the number of days for watering each flower due to the influence of watering the flowers in front. For example, if the number of days for watering the flowers in front is 3, then I + 1, I + 2 flowers get x-day watering flowers for (int I = 0; I <n; I ++) {x + = memory [I]; if (B [I]-x)> 0) {B [I]-= x; tempm-= B [I]; if (tempm <0) {break ;} x + = B [I]; // x can accumulate the number of days for watering from the front. memory [I + w]-= B [I]; // After the length is w, the watering days will be returned.} if (tempm> = 0) l = mid + 1; else r = mid-1 ;} printf ("% d \ n" L-1 );}}