Ural bicolored horses (two-dimensional DP)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.