HDU 1421 Moving Dorm (DP)

Source: Internet
Author: User

Move BedroomTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 1421

Description

Move bedroom is very tired, XHD deep have experience. Date July 9, 2006, the day Xhd forced to move from building 27th to building 3rd, because 10th to seal the building. Looking at the bedroom n items, XHD began to Daze, because N is a less than 2000 integer, is too much, So XHD decided to move 2*k pieces of the past. But still very tired, because 2*k is also not small is an integer not greater than N. Fortunately Xhd based on years of experience in moving things to find that each time the fatigue is in direct proportion to the weight difference of the right hand goods (add a sentence, xhd each move two things , one right hand of the left hand). For example, the XHD left hand with a weight of 3 items, the right hand with a weight of 6 items, then he moved the exhaustion of this time for (6-3) ^2 = 9. Now poor XHD wants to know what the best state of the 2*k is (that is, the lowest degree of fatigue), please tell him.

Input

Each group of input data has two lines, the first row has two number n,k (2<=2*k<=n<2000). The second line has n integers representing the weight of the n item (the weight is a positive integer less than 2^15).

Output

corresponding to each set of input data, the output data only one represents his minimum fatigue, each row.

Sample Input

2 1 1 3

Sample Output

4

Before doing all is the bottom up fill in the DP, this is the first topic from the top down with a memo DP, a bit not accustomed to, think of one or two hours or no idea, reference to the online solution, although I wrote the AC code but have not thoroughly understood, to be strengthened.

Idea: First, for the sorted array, the number of the minimum variance in a number must be adjacent to it, not the previous one is the latter, so should be one to one to take adjacent.

Second, for the sorted array, from the n number of K pairs, first analyze the nth number, this number either take or not take, if you want to take, you must and n-1 a pair, because it said, and it is not in front of it is the one behind it, because it is the nth, it does not exist after the number, So we can only take the first n-1 to make a pair. So you can follow this idea to recursion.

Open a two-dimensional array, according to the convention named Dp,dp[n][k] is to take K pairs from the n number, and then use a one-dimensional array to deposit input values (to sort).

When N==2*k, dp[n][k] = dp[n-2][k-1] + (S[n]-s[n-1]) ^2 because all the numbers are taken, there is no need to compare anything.

When n > 2*k, dp[n][k] = min (dp[n-2][k-1] + (S[n]-s[n-1]) ^2,dp[n-1][k]) either take nth and n-1 to form a pair, or discard the nth re-count.

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 #defineMAX 20055 #defineINF 21474836406 7 intDp[max][max/2];8 intS[max];9 Ten intFxintNintk); One intMy_min (intIintj); A intCompConst voidAConst void*b); - intMainvoid) - { the     intn,k; -  -      while(SCANF ("%d%d", &n,&k)! =EOF) -     { +memset (dp,-1,sizeof(DP)); -  +          for(inti =1; I <= n;i + +)//Subscript starting from 1 Ascanf"%d",&s[i]); at  -Qsort (&s[1],n,sizeof(s[0]), comp); - FX (n,k); -printf"%d\n", Dp[n][k]); -     } -  in     return    0; - } to  + intFxintNintk) - { the     if(Dp[n][k]! =-1) *         returnDp[n][k]; $ Panax Notoginseng     if(k = =0|| n = =0) -DP[N][K] =0; the     Else    if(N <2*k) +DP[N][K] =INF; A     Else    if(n = =2*k) theDP[N][K] = FX (n-2K1) + (S[n]-s[n-1]) * (S[n]-s[n-1]); +     Else     -Dp[n][k] = my_min (FX (N-1, K), FX (N-2K1) + (S[n]-s[n-1]) * (S[n]-s[n-1])); $      $     returnDp[n][k]; - } -  the intMy_min (intIintj) - {Wuyi     returnI < J?i:j; the } -  Wu intCompConst voidAConst void*b) - { About     return*(int*) A-* (int*) b; $}
View Code

HDU 1421 Moving Dorm (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.