HDU 2195 monotone se Min [DP]

Source: Internet
Author: User

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/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.