Well... When noip drops, you need to brush the water...
DP question
F [I] [j] indicates the number of solutions for the I-th ox.
F [I] [j] = f [I-1] [j-1] + v [I] (0 <j <k) or f [I-1] [j '] (j = 0 and 0 <j' <k)
First... The first dimension can be rolled out, but the complexity is still O (n * k)
So I thought of a monotonous queue. q [I] indicates the position in the queue where I was selected.
Then, the complexity O (n )~!
1 /************************************** * *********************** 2 Problem: 2442 3 User: rausen 4 Language: C ++ 5 Result: Accepted 6 Time: 64 MS 7 Memory: 1976 kb 8 ************************************* * *************************/9 10 # include <cstdio> 11 # include <algorithm> 12 13 using namespace std; 14 typedef long ll; 15 const int N = 100005; 16 int n, k, l, r; 17 ll f [N], ans, tot; 18 int q [N]; 19 20 inline int read () {21 int x = 0; 22 char ch = getchar (); 23 while (ch <'0' | ch> '9') 24 ch = getchar (); 25 while (ch> = '0' & ch <= '9') {26 x = x * 10 + ch-'0'; 27 ch = getchar (); 28} 29 return x; 30} 31 32 int main () {33 n = read (), k = read (); 34 int I, x; 35 ans = (ll) 1e15; 36 for (I = 1; I <= n; ++ I) {37 tot + = (x = read ()); 38 f [I] = x + f [q [l]; 39 while (f [q [r]> f [I] & l <= r) -- r; 40 q [++ r] = I; 41 while (q [l] <I-k) + + l; 42 if (I> = n-k) 43 ans = min (ans, f [I]); 44} 45 printf ("% lld \ n", tot-ans); 46 return 0; 47}View Code
(P.s., who told me how the giant "zhonghaoxi" is 28ms... I always think it is unscientific, Ah !)
BZOJ2442 [Usaco2011 Open] lawn trimming