首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 3480 Division

HDU 3480 Division

作者头像
attack
发布2018-04-11 10:46:04
6490
发布2018-04-11 10:46:04
举报

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

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

Recommend

zhengfeng   |   We have carefully selected several similar problems for you:  3478 3485 3487 3486 3484

四边形不等式好恶心。。

首先对所有的数据排序(根据方差的性质贪心)

我们用dp[i][j]表示前j个数,分为i段的最小代价

朴素的转移的话枚举前一段的断点

然后根据……&*()¥#%……&我们可以知道这玩意儿满足四边形不等式

然后愉快的套上板子就好啦

#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;//边界 
            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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-02-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档