Http://poj.org/problem? Id = 2018
Given the number of N, you can find the interval with the maximum length equal to or greater than the average value of F.
This topic is described in detail in this paper. Let's take a look at this article. I will explain the key steps.
1: The sum of a range can be expressed as (s [B]-s [A-1])/(B-A + 1), similar to the slope of two points, that is(A-1, s [A-1]) (B, s [B])The slope of two points can be expressed.[A, B]Range and, S is the prefix and
2: The problem is to convert it to give you n coordinate points to find the two points with the greatest slope after the link x distance is greater than F.
3: maintain a lower convex line using a method similar to maintaining a convex bag. This line is monotonic, that is, the point on the left of the current optimal line cannot be better than it in the future and can be deleted, therefore, maintaining two pointers indicates the two points on the left and right of the line. Each point can be added or deleted at most once. The complexity is O (n );
# include
# include
# include
using namespace STD; const int maxn = 100010; struct point {Double X, Y;} Q [maxn]; double cross (point a, point B, point C) {return (B. x-a.x) * (C. y-a.y)-(C. x-a.x) * (B. y-a.y);} double sum [maxn]; double getk (point a, point B) {return (B. y-a.y)/(B. x-a.x);} int main () {int N, F; while (scanf ("% d", & N, & F )! = EOF) {sum [0] = 0; For (INT I = 1; I <= N; I ++) {scanf ("% lf ", & sum [I]); sum [I] + = sum [I-1];} int front = 0, tail = 0; int ans = 0; For (INT I = F; I <= N; I ++) {point TMP; TMP. X = I-f; TMP. y = sum [I-f]; while (tail-front> = 2 & cross (Q [tail-2], Q [tail-1], TMP) <0) tail --; Q [tail ++] = TMP; point now; now. X = I, now. y = sum [I]; Double K = getk (Q [Front], now); While (tail-front> 0 & getk (Q [Front + 1], now)> K) {k = getk (Q [Front + 1], now); front ++;} If (1000 * k> ans) ans = 1000 * k ;} printf ("% d \ n", ANS);} return 0 ;}