UVA10271 "Chopsticks"

Source: Internet
Author: User

UVA10271 "Chopsticks" Description:

In PRC, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different. He Usesa set of three chopsticks–one pair, plus an EXTRA long chopstick to get some big food by piercing it through the Food. As may guess, the length of the shorter chopsticks should is as close aspossible, but the length of the extra one is not important, as long as it's the longest. To make things
Clearer, for the set of chopsticks with lengths A, B, C (A≤b≤c), (A? B) * (A? B) is called the "badness" of the set. It ' s December 2nd, MR.L ' s birthday! He invited K people to the join his birthday party, and would liketo introduce his-to-using chopsticks. So, he should prepare K + 8 sets of chopsticks (for Himself,his wife, his little son, little daughter, his mother, father, Mother-in-law, father-in-law, and K other
Guests). But MR.L suddenly discovered it chopsticks is of quite different lengths! He should find a-a-composing the K + 8 sets, so and the total badness of the sets are minimized.

Input

The first line is the input contains a single integer T, indicating the number of the test cases (1≤T≤20).
Each test case begins with integers K, N (0≤k≤1000, 3K + 24≤n≤5000), the number of guests and the number of CH Opsticks.
There is N positive integers Li on the next line in Non–decreasing order indicating the lengths of the chopsticks (1≤li ≤32000).

Output

For each test case in the input, print a line containing the minimal total badness of all the sets.

Note:

For the sample input, a possible collection of the 9 sets is:
8,10,16; 19,22,27; 61,63,75; 71,72,88; 81,81,84; 96,98,103; 128,129,148; 134,134,139; 157,157,160

Sample Input

1
1 40
1 8 10 16 19 22 27 33 36 40 47 52 56 61 63 71 72 75 81 81 84 88 96 98
103 110 113 118 124 128 129 134 134 139 148 157 157 160 162 164

Sample Output

23

Translation:

There are n data, given k, from which to select K+8 triples (x, Y, Z, where X<=y<=z), each time the cost is (x-y) ^2, the minimum cost and.

Solution:

DP
Status: F[i][j] indicates that the number of first I is selected by the J pair, (ans) min.
Transfer equation: F[i][j]=min (f[i-1][j],f[i-2][j-1]+ (a[i]-a[i-1)) ^2);
Tips :
1. Given a non-descending sequence of a[], so as to avoid the sort.
2.1<=i <=n-1
3.1<=j <=k+8
4.3*j <= I cannot enumerate all cases, such as i=4,j=2 cannot be established, because third chopsticks need to be considered
5. The clever way is to inverted input a[i], so that the previous number must be greater than the number of the following, that is guaranteed Z>max (x, y)
Code:

#include <stdio.h>#include <string.h>#define MAXN 6000#define INF 99999intT,n,k;inta[maxn],f[maxn][1700];intMinintAintb) {returnA&LT;B?A:B;}intcmpintAintb) {returnA<b?1:0;}intMain () {scanf("%d", &t); while(t--) {memsetA0,sizeof(a));scanf("%d%d", &k,&n); for(inti=n;i>=1; i--) {scanf("%d", &a[i]); } for(intI=1; i<=n;i++) {f[i][0]=0; for(intj=1; j<=k+8; j + +) {F[i][j]=inf; }        } for(intI=1; i<=n;i++) { for(intj=1; j<=k+8; j + +) {if(3*j<=i) F[i][j]=min (f[i-1][j],f[i-2][j-1]+ (a[i]-a[i-1]) * (a[i]-a[i-1])); }        }printf("%d\n", f[n][k+8]); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA10271 "Chopsticks"

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.