HDU 1421 Classic DP

Source: Internet
Author: User

Obviously, after the items are sorted by weight, if you want to move a pair of items, these two items must be adjacent.

Then the order of the first item to consider the article I put or not put, that is to get the state transfer equation:

F[i][j] = min (f (i-1, J), F (i-2, j-1) + d[i]);

Among them, D[i] indicates the fatigue caused by the pair of articles I and article i-1.

1#include <algorithm>2#include <cstring>3#include <cstdio>4 using namespacestd;5 6 Const intN =2001;7 Const intM =1001;8 intF[n][m];9 intC[n];Ten intN, K; One  A intFintIintj) - { -     if(J *2> i)return 999999999; the     if(i = =0|| i = =1)return 0; -     returnF[i][j]; - } -  + intMain () - { +      while(SCANF ("%d%d", &n, &k)! =EOF) A     { at          for(inti =1; I <= N; i++ ) -         { -scanf"%d", C +i); -         } -Sort (c +1, C +1+n); -          for(inti =2; I <= N; i++ ) in         { -              for(intj = I/2; J >0; j-- ) to             { +                 intD = (C[i]-c[i-1]) * (C[i]-c[i-1] ); -F[i][j] = min (F (i-1, j), F (i-2, J-1) +d); the             } *         } $printf"%d\n", F[n][k]);Panax Notoginseng     } -     return 0; the}

HDU 1421 Classic 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.