Bicolored horses
N horses and K stables for you. Each horse will only be 0 or 1, there will be an unhappy value in each horse store (the unhappy value = the number of 0 horses * The number of 1 horses). How can we obtain a minimum unhappy value, output the smallest unhappy value.
Idea: First (N ^ 2) process the unhappy values in each interval, and then use DP to obtain the minimum unhappy values of K stables.
DP [I] [J], I indicates the number of stables currently allocated, and J indicates the number of horses currently allocated.
/*************************************** * *********************************> File Name: ural1167.cpp> author: glsilence> created time: ******************************** **************************************** /# include <stdio. h ># include <iostream> using namespace STD; const int INF = 0x7ffffff; int A [505]; int B [505] [505], C [505] [505]; int sum [505] [505]; int DP [505] [505]; int main () {int N, K; scanf ("% d", & N, & K); For (INT I = 1; I <= N; ++ I) {scanf ("% d", & A [I]); DP [0] [I] = inf;} For (INT I = 1; I <= N; ++ I) {for (Int J = I; j <= N; + + J) {if (a [J] = 0) {B [I] [J] = B [I] [J-1] + 1, c [I] [J] = C [I] [J-1];} else {B [I] [J] = B [I] [J-1], c [I] [J] = C [I] [J-1] + 1 ;} sum [I] [J] = B [I] [J] * C [I] [J] ;}} int P; For (INT I = 1; I <= K; ++ I) {for (Int J = 1; j <= N; ++ J) {int min = inf; For (INT T = 1; T <= J; + + T) {P = DP [I-1] [T-1] + sum [T] [J]; min = min (min, P );} DP [I] [J] = min ;}} printf ("% d \ n", DP [k] [N]); Return 0 ;}