HDU 3480 Division (Slope optimization + two-dimensional DP)

Source: Internet
Author: User

Division

Time limit:10000/5000 MS (java/others) Memory limit:999999/400000 K (java/others)
Total submission (s): 3984 Accepted Submission (s): 1527


Problem Descriptionlittle D is really interested in the theorem of sets recently. There ' s a problem that confused him a long time.
Let T is a set of integers. Let the MIN is the minimum integer in T and MAX is the maximum, then the cost of set T if defined as (max–min) ^2. Now given a 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.
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 would be given.
For any test case, the first line contains integers N (≤10,000) and M (≤5,000). N is the number of elements in S (could be duplicated). M is the number of subsets, which we want to get. In the next line, there'll be N integers giving set S.

Outputfor each test case, output one line containing exactly one integer and 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:1case 2:18 HintThe answer would fit into a 32-bit signed integer.

Source acm-icpc multi-university Training Contest (5)--host by Bjtu

Ideas

Slope optimization + distributive DP.

Set F[i][j] means the minimum consumption of the first I as a collection of J, there is a transfer equation:

f[i][j]=min{f[k][j-1]+ (a[k]-a[j+1]) ^2}

If there is k>l, and decision K is better than decision L, there are:

F[K][J-1]-F[L][J-1]+SQ (a[k+1])-sq (a[l+1]) <= (a[k+1]-a[l+1]) *a[i]

First J-Loop enumeration f[][j], each layer maintains a monotone queue.

Multiplication time-consuming disparity, if the problem is directly beyond the timeout.

Code

1#include <cstdio>2#include <iostream>3#include <algorithm>4 using namespacestd;5 6typedefDoubledo ;7 Const intN = 1e4+Ten;8 Const intM = the+Ten;9 Ten intF[n][m],a[n],q[n]; One intN,m,l,r; A intSqintx) {returnx*x;} - intUp (intLintKintj) { -     returnf[k][j-1]-f[l][j-1]+SQ (a[k+1])-sq (a[l+1]); the } - intDN (intLintKintj) { -     return 2* (a[k+1]-a[l+1]); - } + voidReadint&x) { -     CharC=getchar (); while(!isdigit (c)) c=GetChar (); +x=0; while(IsDigit (c)) x=x*Ten+c-'0', c=GetChar (); A } at intMain () { -     intT,kase=0; - read (T); -      while(t--) { - read (n), read (m); -          for(intI=1; i<=n;i++) read (A[i]); inSort (A +1, a+n+1); -          for(intI=1; i<=n;i++) f[i][1]=SQ (a[i]-a[1]);//Initialize first layer to          for(intj=2; j<=m;j++) { +L=r=0; -              for(intI=1; i<=n;i++) { the                  while(L<r && Up (q[l],q[l+1],J) <=a[i]*dn (q[l],q[l+1],J)) l++; *                 intt=Q[l]; $f[i][j]=f[t][j-1]+SQ (a[i]-a[t+1]);Panax Notoginseng                  while(L<r && Up (q[r-1],Q[R],J) *dn (q[r],i,j) >=up (q[r],i,j) *dn (q[r-1],Q[R],J)) r--; -q[++r]=i; the             } +         } Aprintf"Case %d:%d\n",++kase,f[n][m]); the     } +     return 0; -}

HDU 3480 Division (Slope optimization + two-dimensional 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.