Subject to the effect:
1. gives the length n of the array, required to be selected as a value between the cost of every m successive elements, find the minimum cost of the array.
Problem solution ideas:
1. apparently linear dp, dp [i] represents a minimum total consideration when selecting the i-th element. Clearly state transition equation of dp [i] = min (dp [j]) + a [i]. (I - m <= j < = i - 1). However, when evaluated min (dp [j]), we need to iterate over a length of m size range, m and n are as large limits. Time of charge of 0 (N ^ 2). Obviously time out.
2. We need monotone queue length is the minimum to maintain the size of the interval m, the minimum HOL namely, direct O (1) to give min (dp [j]), it can be avoided timeout.
code show as below:
. 1 #include <stdio.h> 2 #include <the deque> . 3 #include <algorithm> . 4 const int MAXN 2E5 + = 100 ; . 5 the using namespace STD; . 6 const int INF = 0x3f3f3f3f ; . 7 . 8 int n-, m, A [ MAXN]; . 9 int DP [MAXN]; // represents a minimum total cost of the i-th beacon when the beacon is placed 10 the deque < int > Q; . 11 12 is int main () 13 is { 14 Scanf ( " % D% D" , & N-, & m); 15 for ( int I = . 1 ; I <= n-; I ++ ) 16 Scanf ( " % D " , & A [I]); . 17 for ( int I = . 1 ; I <= m; I ++) // DP initialization and mono queue 18 is { . 19 DP [I] = A [I]; 20 is the while (! Q.empty ()) 21 is { 22 is IF (DP [I] <DP [Q. Back ()]) // assurance team is stored in the head dp value of the minimum index of the tail to the head-of monotonically increasing 23 is Q.pop_back (); 24 the else 25 BREAK ; 26 is } 27 Q.push_back (I); 28 } 29 for ( int I = m + . 1 ; I <= n-; I ++ ) 30 { 31 is the while (! Q.empty ()) 32 { 33 is IF (I - m> Q.front ()) // guaranteed queue head is within the range of enumeration, i.e. in [i - m, i - 1 ] in the range of 34 is Q.pop_front (); 35 the else 36 BREAK ; 37 [ } 38 is DP [I] DP = [Q.front ()] + a[i]; 39 while(!Q.empty()) 40 { 41 if(dp[i] < dp[Q.back()]) 42 Q.pop_back(); 43 else 44 break; 45 } 46 Q.push_back(i); 47 } 48 int ans = inf; 49 for(int i = n; i > n - m; i --) 50 ans = min(ans, dp[i]); 51 printf("%d\n", ans); 52 return 0; 53 } 54 /* 55 5 3 56 1 2 5 6 2 57 58 4 59 */