POJ 3671 Dining Cows (DP)

Source: Internet
Author: User

To give you a string of only 1, 2 of the number, let you change the minimum number of times, so that the sequence becomes non-descending.

Thinking: Dynamic programming, judging the demarcation point, open an array of dp[30010][2], where dp[i][j] means to change the number of I to J at least how many times it takes
Then the state transfer equation is listed:
Make A=1 j! =a[i]
0 J==a[i]
So dp[i][1]=dp[i-1][1]+a;
Dp[i][2]=min (dp[i-1][1],dp[i-1][2]) +a;


#include <iostream>using namespace Std;int dp[30010][2];int main () {    int n;    cin>>n;    for (int i=1; i<=n; i++)    {        int a, b;        cin>>a;        b= (a==1?0:1);        Dp[i][1]=dp[i-1][1]+b;        B=!b;        Dp[i][2]=min (Dp[i-1][1], dp[i-1][2]) +b;    }    Cout<<min (Dp[n][1], dp[n][2]) <<endl;    return 0;}



POJ 3671 Dining Cows (DP)

Related Article

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.