HDU 2195 monotone se Min [DP]
Given a string containing 01, evaluate a monotonic function f to minimize the sum of (f (I)-B (I) ^ 2, 1 <= I <= L, where B [I] is 0 or 1;
Analysis: Obviously, F [I] is between 0 and 1. The answer requires three decimal places. Therefore, we can increase B [I] and f [I] by 1000 times to perform integer operations;
If DP [I] [J] is set to the first I character, F [I] is used to obtain the minimum value of J;
DP [I] [J] = min {DP [I-1] [k] + (J-B [I]) * (J-B [I]), 1 <= k <= J}
Of course we still divide the result by 1000000;
Complexity: O (1000 * l );
Code// HDU 2195 monotone se min <br/> # include <iostream> <br/> # include <algorithm> <br/> # include <cstring> <br/> using namespace STD; <br/> const int max = 250; </P> <p> int L; <br/> char s [Max]; <br/> int DP [Max] [1010]; <br/> void GS () <br/> {<br/> int I, J; <br/> int TT = s [1] = '0 '? 0: 1000; <br/> for (I = 0; I <=; I ++) DP [1] [I] = (TT-I) * (TT-I); <br/> int small; <br/> for (I = 2; I <= L; I ++) <br/>{< br/> small = 0x5fffffff; <br/> TT = s [I] = '0 '? 0: 1000; <br/> for (j = 0; j <=; j ++) <br/> {<br/> If (small> DP [I-1] [J]) Small = DP [I-1] [J]; <br/> DP [I] [J] = Small + (TT-j) * (TT-j ); <br/>}< br/> int main () <br/> {<br/> while (scanf ("% s", S + 1) = 1) <br/>{< br/> L = strlen (S + 1); <br/> Gs (); </P> <p> int ans = 0x5fffffff; <br/> for (INT I = 0; I <= 1000; I ++) ans = min (ANS, DP [l] [I]); <br/> double AA = (double) ans/1000000; <br/> printf ("%. 3lf/N ", AA); <br/>}< br/> system (" pause "); <br/> return 0; <br/>}< br/>