zoukankan      html  css  js  c++  java
  • TImus 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 P1 horses in the first stable, the next P2 in the 2nd stable and so on. Moreover, he doesn't want any of the Kstables 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

    inputoutput
    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.

    题意:一条线上有n匹马(黑马编号为1,白马编号为0)

       现在要把马划分为k条线,分别分在k个马槽,

       问怎么分,使得n匹马的unhappiness最小。

       

       即:

       给出n,k

        长度为n的数组,数组元素为0或1,

       现在问,把这个数组划分为k个连续的子序列(不允许有空的子序列),

       每个子序列的unhappiness=0的数目*1的数目

       数组的unhappiness=所有子序列的uphappiness的和

       现在问,怎么划分可以使得这个数组的unhappiness最小。

     

    dp[i][j] 表示前i个马槽,前j匹马,最小的unhappiness.

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 
     7 const int maxn=503;
     8 const int inf=0x3f3f3f3f;
     9 
    10 int dp[maxn][maxn];
    11 int w[maxn][maxn];
    12 int a[maxn];
    13 
    14 int main()
    15 {
    16     int n;
    17 
    18     while(scanf("%d",&n)!=EOF)
    19     {
    20         int k;
    21 
    22         scanf("%d",&k);
    23 
    24         for(int i=1;i<=n;i++)
    25         {
    26             scanf("%d",&a[i]);
    27         }
    28 
    29         for(int i=1;i<=n;i++)
    30         {
    31             for(int j=i;j<=n;j++)
    32             {
    33                 int bla=0,whi=0;
    34                 for(int t=i;t<=j;t++)
    35                 {
    36                     if(a[t])
    37                         bla++;
    38                     else
    39                         whi++;
    40                 }
    41                 w[i][j]=bla*whi;
    42             }
    43         }
    44 
    45         for(int j=1;j<=n;j++)
    46             dp[1][j]=w[1][j];
    47 
    48         for(int i=2;i<=k;i++)
    49             for(int j=1;j<=n;j++)
    50                 dp[i][j]=inf;
    51 
    52         for(int i=2;i<=k;i++)
    53         {
    54             for(int j=1;j<=n;j++)
    55             {
    56                 for(int t=i-1;t<j;t++)
    57                 {
    58                     dp[i][j]=min(dp[i][j],dp[i-1][t]+w[t+1][j]);
    59                 }
    60             }
    61         }
    62 
    63         printf("%d
    ",dp[k][n]);
    64     }
    65 
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    黑马程序员--C语言基础之--数组、指针的基础知识
    黑马程序员--C语言基础之--变量、内部函数与外部函数
    黑马程序员--C语言基础之--printf函数
    黑马程序员--C语言基础之--scanf函数
    黑马程序员--C语言基础之--数据类型
    鉴定一张图片是否处理过
    图解JQUERY尺寸及位置定义
    谈谈对“渐进增强与平稳退化“的理解
    理解 JavaScript 作用域和作用域链
    编写更好的jQuery代码的建议
  • 原文地址:https://www.cnblogs.com/-maybe/p/4482796.html
Copyright © 2011-2022 走看看