HDU 3480 Division
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: %dn",++cnt,dp[M][N]);
}
return 0;
}
- Spring 必知概念(二)
- 利用iText 组件导出PDF
- 利用POI组件导出Excel的简单示例
- MongoDB 整合spring-data-jpa,spring 4.x
- 【微信开发】 红包接口开发
- 【微信开发】 使用单例设计模式 提供AccessToken 和Jsapi_ticket缓存支持
- @ResponseBody响应JSON 406
- tomcat不能运行或共存多个项目
- Spring Task 定时任务
- 如何为机器学习索引,切片,调整 NumPy 数组
- 逆向工程分析:摩托罗拉安全摄像头究竟有多不安全?
- 【maven学习】 利用Profile构建不同环境的部署包
- 如何写好一份渗透测试报告?
- jqGrid 关闭刷新父页面数据
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- JavaWeb使用德鲁伊(略)实现登录、激活码注册(发送激活码到邮箱,点击激活链接后,才能正常登陆)、注册界面
- 转发的两种方式与重定向
- 关于监控、链路追踪、日志三者的区别
- 10.12面试:SpringMVC静态资源放行+如何实现转发和重定向+如何支持json+设置时间格式+设置json的key+对json的value序列化
- 10.13面试:什么是跨域?如何解决跨域问题+springMVC如何处理文件上传和下载+ssm整合思路 (待完善)
- 查找jar地址
- shiro篇:缓存配置
- 定时器篇:定时任务入门小案例
- 设置SSH + 远程仓库操作
- SSM整合:商城小项目001(配置SSM环境+查询所有操作+添加操作)
- 在MyBatis中参数的传递4种方式+代码示例
- MySQL篇:MySQL5.5和 MySQL5.7.x 版本安装和卸载《从入门到入狱,删库--->你值得拥有》
- 1016面试:quartz的核心组件+cron表达式语法+如何实现一个每天中午12点备份的数据库?写伪代码
- shiro篇:使用Shiro对一个SSM项目进行身份加密验证
- 由Spring创建dao和service