# Division

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

Problem Description
Little 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.

Input
The 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.

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

Sample Input
```2
3 2
1 2 4
4 2
4 7 10 1
```

Sample Output
```Case 1: 1
Case 2: 18

Hint
The answer will fit into a 32-bit signed integer.

```

Source #include <iostream> #include
<cstdio> #include
<algorithm> using namespace std; #define  N   10009 #define  INF 0x3fffffff int n, m, a[ N ]; int solve() { static int f[ 2 ][ N ], s[ 2 ][ N ]; int i, j, k, cur, pre, tmp; cur
= 0; for ( i = 1; i <= n; ++i ) { f[ cur ][ i ]
= (a[i]-a)*(a[i]-a); s[ cur ][ i ]
= 1; } for ( j = 2; j <= m; ++j ) { pre
= cur; cur
^= 1; s[ cur ][ n
+ 1 ] = n - 1; for ( i = n; i >= j; --i ) { f[ cur ][ i ]
= INF; for ( k = s[ pre ][ i ]; k <= s[ cur ][ i + 1 ]; ++k ) { tmp
= f[ pre ][ k ] + (a[i]-a[k+1])*(a[i]-a[k+1]); if ( tmp < f[ cur ][ i ] ) { f[ cur ][ i ]
= tmp; s[ cur ][ i ]
= k; } } } } return f[ cur ][ n ]; } int main() { int tc, cc, i; scanf(
"%d"&tc ); for ( cc = 1; cc <= tc; ++cc ) { scanf(
"%d%d"&n, &m ); for ( i = 1; i <= n; ++i ) { scanf(
"%d", a + i ); } sort( a
+ 1, a + n + 1 ); printf(
"Case %d: %d\n", cc, solve() ); } return 0; } posted on 2011-03-18 09:38 coreBugZJ 阅读(933) 评论(0)  编辑 收藏 引用 所属分类: ACM
{"ft_lang_label":"__label__en","ft_lang_prob":0.6854784,"math_prob":0.9953909,"size":2153,"snap":"2019-51-2020-05","text_gpt3_token_len":738,"char_repetition_ratio":0.11400651,"word_repetition_ratio":0.064449064,"special_character_ratio":0.4115188,"punctuation_ratio":0.16932271,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9992966,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126],"im_url_duplicate_count":[null,3,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-09T19:09:11Z\",\"WARC-Record-ID\":\"<urn:uuid:50ad3fa6-11a0-4941-bcca-1f5221f1a8b8>\",\"Content-Length\":\"84359\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:17a20695-35ab-4dbc-9d84-5d14aa526e82>\",\"WARC-Concurrent-To\":\"<urn:uuid:25def8f1-7e2a-4c99-93fc-c6d40a49ef09>\",\"WARC-IP-Address\":\"47.97.166.246\",\"WARC-Target-URI\":\"http://www.cppblog.com/coreBugZJ/archive/2011/03/18/142101.html\",\"WARC-Payload-Digest\":\"sha1:HF3L4OHR3NMTOM3QEFXOVJB7EUTU3RKP\",\"WARC-Block-Digest\":\"sha1:TSK6CHJ3F4O37FAGEUTZ54C234RD4IKI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540521378.25_warc_CC-MAIN-20191209173528-20191209201528-00207.warc.gz\"}"}