递推DP URAL 1167 Bicolored Horses

 

题目传送门

 1 /*  2  题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy指数是多少  3  状态转移方程:dp[i][l] = min (dp[i][l], dp[i-1][j] + cur * (l - j - cur)) 表示第l匹马要不还在i马棚,或者去新的马棚  4  本题关键在dp初始化INF,由于黑马白马的表示简单,求指数方便  5 */  6 #include <cstdio>  7 #include <iostream>  8 #include <algorithm>  9 #include <cstring> 10 #include <cmath> 11 using namespace std; 12 13 const int MAXN = 5e2 + 10; 14 const int INF = 0x3f3f3f3f; 15 int dp[MAXN][MAXN]; 16 int a[MAXN], sum[MAXN]; 17 18 int main(void) //URAL 1167 Bicolored Horses 19 { 20 //freopen ("L.in", "r", stdin); 21 22 int n, k; 23 while (scanf ("%d%d", &n, &k) == 2) 24  { 25 memset (sum, 0, sizeof (sum)); 26 for (int i=0; i<=k; ++i) 27 for (int j=0; j<=n; ++j) dp[i][j] = INF; 28 for (int i=1; i<=n; ++i) {scanf ("%d", &a[i]); sum[i] = a[i]; sum[i] += sum[i-1];} 29 30 dp[0][0] = 0; 31 for (int i=1; i<=k; ++i) 32  { 33 for (int j=0; j<=n; ++j) 34  { 35 for (int l=j+1; l<=n; ++l) 36  { 37 int cur = sum[l] - sum[j]; 38 dp[i][l] = min (dp[i][l], dp[i-1][j] + cur * (l - j - cur)); 39  } 40  } 41  } 42 43 printf ("%d\n", dp[k][n]); 44  } 45 46 return 0; 47 }

 

你可能感兴趣的:(color)