1167. Bicolored Horses
Time limit: 1.0 second
Memory limit: 64 MB
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
i black horses and
jwhite 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.
Problem Author: Mugurel Ionut Andreica
Problem Source: Romanian Open Contest, December 2001
Problem Source: Romanian Open Contest, December 2001
Difficulty: 143
Printable version
Submit solution
Discussion (29)
All submissions (10857) All accepted submissions (5192) Solutions rating (2926)
题意:给n匹马,k个马厩,将n个马分配到k个马厩中,要求必须是连续的才能分配到一个马厩中,每个马厩有一个值,(0的个数)×(1的个数),求其和最小。
dp[ i ][ j ] : 表示第i个马厩最后一个是第 j匹马;
状态转移方程: dp[i][j] = min(dp[i][j], dp[i - 1][m] + (sum[j].l - sum[m].l)*(sum[j].o - sum[m].o));
注意初始化dp[ i ][ i ] = 0, 其他dp[ i ][ j ] = INF;
代码:
/* dp[ i ][ j ] : 表示第i个马厩最后一个是第 j匹马; 注意初始化dp[i][i] = 0;其他为无穷大; dp[i][j] = min(dp[i][j], dp[i - 1][m] + (sum[j].l - sum[m].l)*(sum[j].o - sum[m].o)); */ #include<bits/stdc++.h> #define ll long long using namespace std; const int maxn = 5e2+7; int INF = 100000; int n, k, color[maxn], dp[maxn][maxn]; struct node{ int o, l; // 0->o; l->1; }sum[maxn]; void init() { sum[0].o = 0, sum[0].l = 0; for(int i = 1; i <= n; i++) { sum[i].l = sum[i - 1].l; sum[i].o = sum[i - 1].o; if(color[i] == 0) sum[i].o++; else sum[i].l++; } for(int i = 0; i <= k; i++) { for(int j = 0; j <= n; j++) dp[i][j] = INF; dp[i][i] = 0; } } int main() { //freopen("in.txt", "r", stdin); while(~scanf("%d%d",&n, &k)) { for(int i = 1; i <= n; i++) scanf("%d", &color[i]); init(); 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][j], dp[i - 1][m] + (sum[j].l - sum[m].l)*(sum[j].o - sum[m].o)); } } } printf("%d\n", dp[k][n]); } return 0; }