Http://acm.timus.ru/problem.aspx? Space = 1 & num = 1167
There are n horses, black and white. Put them in K stables in sequence, and put X horses in a stables. the unhappy value is the number of horse horses * The number of white horses. What is the minimum unhappy value?
Set DP [I] [J] to indicate the minimum unhappy value of J horse in the previous horse store, so DP [I] [J] = min (DP [I-1] [g] + TMP [G + 1] [J]).
TMP is the unhappy value of the pre-processed I to J horse.
# Include <stdio. h> # include <iostream> # include <map> # include <set> # include <list> # include <stack> # include <vector> # include <math. h> # include <string. h> # include <queue> # include <string> # include <stdlib. h >#include <algorithm> # define ll _ int64 # define EPS 1e-12 # define PI ACOs (-1.0) # define pp pair <LL, ll> using namespace STD; const int INF = 0x3f3f3f; const int maxn = 100010; const int mod = 1000000007; int N, K; int A [510]; int TMP [510] [510]; int T [2]; int DP [510] [510]; int main () {While (~ Scanf ("% d", & N, & K) {for (INT I = 1; I <= N; I ++) {scanf ("% d", & A [I]) ;}for (INT I = 1; I <n; I ++) {T [0] = T [1] = 0; t [A [I] + = 1; TMP [I] [I] = 0; for (Int J = I + 1; j <= N; j ++) {T [A [J] + = 1; TMP [I] [J] = T [0] * t [1] ;}} memset (DP, INF, sizeof (DP); For (INT I = 1; I <= N; I ++) {DP [1] [I] = TMP [1] [I];} For (INT I = 2; I <= K; I ++) {for (Int J = I; j <= N; j ++) {int min = inf, Pos; For (INT G = I-1; G <= J; G + +) {If (G + 1 <= J) min = min (Min, DP [I-1] [g] + TMP [G + 1] [J]); // check for a long time. Put this error out of the loop .} DP [I] [J] = min ;}} printf ("% d \ n", DP [k] [N]);} return 0 ;}
Ural bicolored horses (two-dimensional DP)