Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

DP,贴代码,飘过~~

Posted by yingxiang720 at 2011-03-30 02:10:46 on Problem 3671
#include <iostream>
using namespace std;

int cow[30001];
int dp[30001];
int main()
{
    int n,i,cmin;
    while(scanf("%d",&n) != EOF)
    {
        for(i = 0;i < n;i++)    scanf("%d",&cow[i]);
        dp[0] = 0;
        dp[n] = 0;
        for(i = 0;i < n;i++)
            if(cow[i] == 1) dp[0] ++;
        for(i = 1;i < n;i++)
        {
            if(cow[i - 1] == 1) dp[i] = dp[i - 1] - 1;
            if(cow[i - 1] == 2) dp[i] = dp[i - 1] + 1;
        }
        for(i = 0;i < n;i++)
            if(cow[i] == 2) dp[n] ++;
        cmin = dp[0];
        for(i = 1;i <= n;i++)
            cmin = cmin > dp[i] ? dp[i] : cmin;
        for(i = n - 1;i >= 0;i--)
            if(cow[i] == 1) dp[n-1]++;
        for(i = n - 2;i >= 0;i--)
        {
            if(cow[i + 1] == 1) dp[i] = dp[i + 1] - 1;
            if(cow[i + 1] == 2) dp[i] = dp[i + 1] + 1;
        }
        for(i = 1;i < n;i++)
            cmin = cmin > dp[i] ? dp[i] : cmin;
        printf("%d\n",cmin);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator