HDU 1421 dormitory migration-type backpack Problems

Source: Internet
Author: User
/* It can be said that it is a problem with the backpack: the size of the backpack is K groups. K groups from N must be adjacent to two groups due to the smallest square difference. There are n-1 groups in total, finding the square difference between each group changes to the number of K hops and the smallest number of N-1 records (it seems simple, but you cannot move the same item twice, when we calculate the square difference between two adjacent numbers, except the first and the last, the other items are calculated twice.) Therefore, we use dynamic planning */# include <stdio. h ># include <algorithm> using namespace STD; # define min (A, B) (a) <(B )? (A) :( B) int d [2005] [1005], dat [2005]; int N, K; void dp () {int I, j; memset (D, 127, sizeof (d); for (I = 0; I <= N; I ++) d [I] [0] = 0; for (I = 2; I <= N; I ++) for (j = 1; j * 2 <= I; j ++) d [I] [J] = min (d [I-1] [J], d [I-2] [J-1] + dat [I-1]); // if the current group is selected (the square difference between I and I-1 exists dat [I-1]), // then dat [I-1] (the square difference between the current I and i-1) + d [I-2] [J-1] (pick a I-1 group in the J-1 to ensure that no heavy item will be picked)} int main () {int I; while (scanf ("% d", & N, & K )! =-1) {for (I = 1; I <= N; I ++) scanf ("% d", & dat [I]); sort (DAT + 1, DAT + n + 1); for (I = 1; I <n; I ++) DAT [I] = (DAT [I]-dat [I + 1]) * (DAT [I]-dat [I + 1]); DP (); printf ("% d \ n", d [N] [k]);} return 0 ;}

 

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.