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
题意:给你n个数字,要分成m个子集合,子集合之间可以共用相同的元素,子集合的代价为该集合最大的数和最小的数的差的平方,问你最小的子集合的总代价是多少。
思路:因为集合里的数的顺序并不是按顺序的,所以可以先排序,然后用dp[i][j]表示前i个数分成j个集合所得到的最小代价,和邮局那题有点相似,要用四边形优化,不然会超时,看别人的写法发现斜率优化更快,学完后再来写吧。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 999999999
int a[10060],dp[10060][5060],s[10060][5060];
int main()
{
int n,m,i,j,T,len,k,num1=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+1,a+1+n);
for(i=1;i<=n;i++){
dp[i][1]=(a[i]-a[1])*(a[i]-a[1]);
s[i][1]=1;
}
for(j=2;j<=m;j++){
dp[j][j]=0;
s[n+1][j]=n;
for(i=n;i>j;i--){
dp[i][j]=inf;
for(k=s[i][j-1];k<=s[i+1][j];k++){
if(dp[i][j]>dp[k][j-1]+(a[i]-a[k+1])*(a[i]-a[k+1])){
dp[i][j]=dp[k][j-1]+(a[i]-a[k+1])*(a[i]-a[k+1]);
s[i][j]=k;
}
}
}
}
num1++;
printf("Case %d: %d\n",num1,dp[n][m]);
}
return 0;
}

hdu3480 Division的更多相关文章

  1. HDU3480 Division —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-3480 Division Time Limit: 10000/5000 MS (Java/Others)    Memory ...

  2. hdu3480 Division(dp平行四边形优化)

    题意:将n个数分成m段,每段的代价为最大值减最小值的平方,为代价最小是多少n<=10000 ,m<=5000 题解:先拍好序,从小到大,这样绝对是花费最小的,不过怎么样来做呢?一定很容易想 ...

  3. HDU-3480 Division (四边形不等式优化DP)

    题目大意:将n个数分成m组,将每组的最大值与最小值的平方差加起来,求最小和. 题目分析:先对数排序.定义状态dp(i,j)表示前 j 个数分成 i 组得到的最小和,则状态转移方程为dp(i,j)=mi ...

  4. [HDU3480] Division [四边形不等式dp]

    题面: 传送门 思路: 因为集合可以无序选择,所以我们先把输入数据排个序 然后发先可以动归一波 设$dp\left[i\right]\left[j\right]$表示前j个数中分了i个集合,$w\le ...

  5. HDU3480 Division——四边形不等式或斜率优化

    题目大意 将N个数分成M部分,使每部分的最大值与最小值平方差的和最小. 思路 首先肯定要将数列排序,每部分一定是取连续的一段,于是就有了方程 $\Large f(i,j)=min(f(i-1,k-1) ...

  6. HDU2829 Lawrence —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-2829 Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  7. HDU3480:Division——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=3480 将一列数划分成几个集合,这些集合的并集为该数列,求每个数列的(最大值-最小值)^2的和的最小值. 简单的d ...

  8. python from __future__ import division

    1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/&qu ...

  9. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

随机推荐

  1. 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)

    Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...

  2. 【Jboss】一台服务器上如何部署多个jboss

    一台服务器上如何部署多个jboss呢?直接把整个部署环境copy一份到相应的目录下? 这样只是前提,但是启动复制后的jboss就会发现,有很多端口被占用 3873,8080,8009,8443,808 ...

  3. 【Oracle】Oracle 10g下载路径

    ORACLE 10g下载地址 下载方法: 直接复制下面的链接,打开迅雷,自动会识别下载的内容 Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise ...

  4. ctfhub技能树—web前置技能—http协议—基础认证

    打开靶机环境 下载附件后发现是常用密码字典,于是考虑本题可能是考察密码爆破 打开环境 发现需要认证,于是考虑使用暴力破解 使用burpsuite抓包,查看 发现最下面一行有加密后的密文 使用base6 ...

  5. ctfhub技能树—sql注入—UA注入

    手注 打开靶机 查看页面信息 抓取数据包 根据提示注入点在User-Agent文件头中 开始尝试注入 成功查到数据库名 查询数据表名 查询字段名 查询字段信息 成功拿到flag 盲注 测试是否存在时间 ...

  6. mysql—make_set函数

    使用格式:MAKE_SET(bits,str1,str2,-) 1 返回一个设定值(含子字符串分隔字符串","字符),在设置位的相应位的字符串.str1对应于位0,str2到第1位 ...

  7. 安装macosx10.13high serria

    本教程所需资源下载链接: 链接:https://pan.baidu.com/s/1wGTezXz6zGvtlwpv6mMoSg 提取码:r6n9 安装VMware workstation 16.0,安 ...

  8. django form和form组件

    form介绍: 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是 ...

  9. 针对Fluent-Bit采集容器日志的补充

    hello,之前我写过<一套标准的ASP.NET Core容器化应用日志收集分析方案>,在公司团队.微信公众号.Github上反映良好. 其中配置Fluent-bit使用Forward协议 ...

  10. Centos 7.x系统下忘记用户登录密码,重置root密码的方法

    转载的,作为一个参考保存.谢谢:https://blog.csdn.net/userpass_word/article/details/81807316 1.开机后进入以下界面,然后按Esc或者E键编 ...