Division(hdu 3480)

Division

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 999999/400000K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 8
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.[/hint]

题意
: 给出N个数,要求将N个数分成K个集合(一种划分),使每个集合的(最大值 - 最小值)^ 2和达到最小。

思路

先将a[ ]升序排序,因为这样的话对于区间[x,y],最大值为a[y],最小值为a[x] ;

dp[i][j]:排序后把前j个数分成i个集合最小平方和 ;

状态转移方程:dp[i][j] = min( dp[i-1][k] + (a[j] - a[k+1])^2 )

=> 2*a[j]*a[k+1] + dp[i][j]  - a[j]*a[j] = dp[i-1][k] + a[k+1]*a[k+1]  ;

=>设 x: a[k+1] ,  b: dp[i][j]  - a[j]*a[j] ,  y: dp[i-1][k] + a[k+1]*a[k+1] ;

=>k*x+b=y  =>其中斜率k=2*a[j] ;

斜率计算: k = (y1-y2) / (x1-x2) ;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

const int MAXN=10000+100;
const int MAXM=5000+100;

long long a[MAXN];
long long q[MAXN];
long long dp[MAXM][MAXN];

long long up(int i,int k1,int k2)
{
    return (dp[i-1][k1]+a[k1+1]*a[k1+1]-dp[i-1][k2]-a[k2+1]*a[k2+1]);
}

long long down(int k1,int k2)
{
    return (a[k1+1]-a[k2+1]);
}

int main()
{
    int T;
    scanf("%d",&T);
    int Case=1;
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        printf("Case %d: ",Case++);

        for(int i=1;i<=n;i++)
            dp[1][i]=(a[i]-a[1])*(a[i]-a[1]);

        for(int i=2;i<=m;i++)
        {
            int Left=1;
            int Right=1;
            
            // 细节
            q[1]=i-1;
            
            for(int j=i;j<=n;j++)
            {
                while(Left<Right&&up(i,q[Left+1],q[Left])<=2*a[j]*down(q[Left+1],q[Left]))
                    Left++;
                int Front=q[Left];
                dp[i][j]=dp[i-1][Front]+(a[j]-a[Front+1])*(a[j]-a[Front+1]);
                while(Left<Right&&up(i,q[Right],q[Right-1])*down(j,q[Right])>=up(i,j,q[Right])*down(q[Right],q[Right-1]))
                    Right--;
                q[++Right]=j;
            }
        }
        printf("%lld\n",dp[m][n]);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值