1167. Bicolored Horses(dp)

  1. 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 P1 horses in the first stable, the next P2 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 i 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
    Notes
    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.
    题意:
    每天,农民Ion(这是罗马尼亚人的名字)都会取出他所有的马匹,所以他们可以跑步和玩耍。当他们完成后,农民Ion必须把所有的马带回马厩。为了做到这一点,他把他们排成一条直线,他们跟着他去了马厩。因为他们非常疲倦,农民Ion决定他不想让马匹移动得比他们应该的更多。所以他开发了这个算法:他将第一个P 1马放在第一个稳定中,下一个P 2放在第二个稳定中,依此类推。此外,他不希望他所拥有的任何K马厩都是空的,并且不得将马留在外面。现在你应该知道,农民离子只有黑色或白色的马匹,它们并没有真正相处得很好。如果有我黑马和j匹马在一个稳定,然后该稳定的不幸福系数是i * j。不幸福的总系数是每个K稳定系数的不幸系数的总和。
    确定将N匹马放入K马厩的方法,以便使不幸的总系数最小化。
    dp[i][j]表示用i个马厩安置j匹马
    i-1 个马厩安置t匹马,剩下的马安置到一间
    状态转移方程dp[i][j]=dp[i-1][j-1]+unhappy(剩下的马);
    注意初始化dp INF dp[0][0]=0
#include <stdio.h>
#include <math.h>
#include <queue>
#include <memory.h>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int b[505],unhappy[505][505];
int dp[505][505];
int num_unhappy(int x,int y)//求i,j之间的unhappy值
{
    int a1,b1;
    a1=b[y]-b[x-1];//黑马数量
    b1=y-x+1-a1;//白马
    return a1*b1;
}
int main()
{
    int n,k,i,j,t,x;
    memset(dp,INF,sizeof(dp));
    memset(b,0,sizeof(b));
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&x);
        b[i]=b[i-1]+x;
    }
    for(i=1;i<=n;i++)
    {
        for(j=i;j<=n;j++)
        {
            unhappy[i][j]=num_unhappy(i,j);
        }
    }
    dp[0][0]=0;
    for(i=1;i<=k;i++)//马厩
    {
        for(j=1;j<=n;j++)//马
        {
            for(t=1;t<=j;t++)
            {
                dp[i][j]=min(dp[i][j],dp[i-1][t-1]+unhappy[t][j]);
            }
        }
    }
    printf("%d\n",dp[k][n]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值