Ural 1167. Bicolored Horses DP

1167. Bicolored Horses

Time limit: 1.0 second
Memory limit: 64 MB
Every day, farmer Ion (this is a Romanian name) takes out all his horses, so they may run and play. When they are done, farmer Ion has to take all the horses back to the stables. In order to do this, he places them in a straight line and they follow him to the stables. Because they are very tired, farmer Ion decides that he doesn't want to make the horses move more than they should. So he develops this algorithm: he places the 1st P 1 horses in the first stable, the next P 2 in the 2nd stable and so on. Moreover, he doesn't want any of the  K stables he owns to be empty, and no horse must be left outside. Now you should know that farmer Ion only has black or white horses, which don't really get along too well. If there are  black horses and  j white horses in one stable, then the coefficient of unhappiness of that stable is  i*j. The total coefficient of unhappiness is the sum of the coefficients of unhappiness of every of the K stables.
Determine a way to place the  N horses into the  K stables, so that the total coefficient of unhappiness is minimized.

Input

On the 1st line there are 2 numbers:  N (1 ≤ N ≤ 500) and  K (1 ≤ K ≤ N). On the next N lines there are N numbers. The i-th of these lines contains the color of the i-th horse in the sequence: 1 means that the horse is black, 0 means that the horse is white.

Output

You should only output a single number, which is the minimum possible value for the total coefficient of unhappiness.

Sample

input output
6 3
1
1
0
1
0
1
2

Hint

Place the first 2 horses in the first stable, the next 3 horses in the 2nd stable and the last horse in the 3rd stable.
Problem Author: Mugurel Ionut Andreica
Problem Source: Romanian Open Contest, December 2001
题意:把一个长度为n的长串分成k段连续的子串,使得子串中0的个数乘以1的个数最小。
转移方程:dp[i][j]=min( dp[i-1][m]+(cnt1[j]-cnt1[m])*(cnt0[j]-cnt0[m]) , dp[i][j]  );dp[i][j]表示前i个子串中0*1的值,cnt0表示0的个数,cnt1表示1的个数
#include <iostream>
#include <cstdio>
using namespace std;
#define INF 0x7fffffff;
int n,k;
long long dp[510][510];
int num[510];
int cnt1[510],cnt0[510];
int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    for(int i=0; i<510; i++)
    {
        for(int j=0; j<510; j++)
            if(i==j)
                dp[i][j]=0;
            else
                dp[i][j]=INF;
    }
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&num[i]);
        cnt1[i]=cnt1[i-1]+num[i];
        cnt0[i]=cnt0[i-1]+1-num[i];
    }
    for(int i=1; i<=k; i++)
    {
        for(int j=i; j<=n-(k-i); j++)
        {
            for(int m=i-1; m<j; m++)
            {

                dp[i][j]=min( dp[i-1][m]+(cnt1[j]-cnt1[m])*(cnt0[j]-cnt0[m]) , dp[i][j]  );
            }
        }
    }
    printf("%I64d\n",dp[k][n]);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值