HDU 3480 Division, hdu3480division

Source: Internet
Author: User

HDU 3480 Division, hdu3480division
Division

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others)
Total Submission (s): 5344 Accepted Submission (s): 2115


Problem DescriptionLittle D is really interested in the theorem of sets recently. There's a problem that confused him a long time.
Let T be a set of integers. let the MIN be the minimum integer in T and MAX be the maximum, then the cost of set T if defined as (MAX-MIN) ^ 2. now given an integer set S, we want to find out M subsets S1, S2 ,..., SM of S, such that



And the total cost of each subset is minimal.

 

InputThe input contains multiple test cases.
In the first line of the input there's an integer T which is the number of test cases. Then the description of T test cases will be given.
For any test case, the first line contains two integers N (≤ 10,000) and M (≤ 5,000 ). N is the number of elements in S (may be duplicated ). M is the number of subsets that we want to get. in the next line, there will be N integers giving set S.

 

 

OutputFor each test case, output one line containing exactly one integer, the minimal total cost. Take a look at the sample output for format.

 

 

Sample Input23 21 2 44 24 7 10 1

 

Sample OutputCase 1: 1 Case 2: 18 HintThe answer will fit into a 32-bit signed integer.

 

Source2010 ACM-ICPC Multi-University Training Contest (5) -- Host by BJTU

 

Recommendzhengfeng | We have carefully selected several similar problems for you: 3478 3485 3487 3486 Quadrilateral inequality is disgusting .. First, sort all the data (Greedy according to the nature of the variance) We use $ dp [I] [j] $ to represent the first $ j $ number, which is the minimum cost of the $ I $ segment. For simple transfer, enumerate the breakpoints in the previous section. Then, according ...... & * () ¥ # % ...... & We can know that this stuff satisfies the Quadrilateral inequality. Then, just put it on the board happily. 
# Include <cstdio> # include <cstring> # include <algorithm> const int MAXN = 10001, INF = 1e9 + 10; using namespace std; inline int read () {char c = getchar (); int x = 0, f = 1; while (c <'0' | c> '9 ') {if (c = '-') f =-1; c = getchar () ;}while (c> = '0' & c <= '9 ') {x = x * 10 + c-'0'; c = getchar ();} return x * f;} int dp [MAXN] [MAXN], s [MAXN] [MAXN], a [MAXN]; int mul (int x) {return x * x;} int main () {int Test = read (), cnt = 0; while (Test --) {int N = read (), M = read (); for (int I = 1; I <= N; I ++) a [I] = read (); sort (a + 1, a + N + 1); for (int I = 1; I <= N; I ++) dp [1] [I] = mul (a [I]-a [1]), s [1] [I] = 1; for (int I = 2; I <= M; I ++) {s [I] [N + 1] = N-1; // boundary for (int j = N; j> = I; j --) {int mn = INF, mnpos =-1; for (int k = s [I-1] [j]; k <= s [I] [j + 1]; k ++) {if (dp [I-1] [k] + mul (a [j]-a [k + 1]) <mn) {mn = dp [I-1] [k] + mul (a [j]-a [k + 1]); mnpos = k ;}} dp [I] [j] = mn; s [I] [j] = mnpos;} printf ("Case % d: % d \ n", ++ cnt, dp [M] [N]);} 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.