题目链接:https://vjudge.net/problem/HDU-3480

Division

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others)
Total Submission(s): 5304    Accepted Submission(s): 2093

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
 
Recommend
zhengfeng

题意:

给出一组数,把这组数分成m个集合,使得每个集合的(MAX-MIN)^2的和最小。

题解:

1.首先可以确定:每个集合的数值跨度应该尽量小,所以可以先对这些数进行排序,被分成一组的数必定是相连的。

2.设dp[i][j]为:第j个数属于第i个集合时的最小值,那么:dp[i][j] = min(dp[i-1][k] + (val[i] - val[k+1)^2),其中 i-1<=k<=j-1。

3.根据上述的状态转移方程,可算得时间复杂度为O(n^3),无法接受。因此可以用斜率优化。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e4+; int val[MAXN], dp[MAXN][MAXN];
int q[MAXN], head, tail; int getUP(int i, int k1, int k2)
{
return (dp[i-][k1] + val[k1+]*val[k1+])-
(dp[i-][k2] + val[k2+]*val[k2+]);
} int getDOWN(int k1, int k2)
{
return *(val[k1+]-val[k2+]);
} int getDP(int i, int j, int k)
{
return dp[i-][k] + (val[j]-val[k+])*(val[j]-val[k+]);
} int main()
{
int n, m, T;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d", &n,&m);
for(int i = ; i<=n; i++)
scanf("%d", &val[i]); sort(val+, val++n);
for(int i = ; i<=n; i++) //初始化第一段
dp[][i] = (val[i]-val[])*(val[i]-val[]);
for(int i = ; i<=m; i++) //从i-1段转移到i段
{
head = tail = ;
q[tail++] = i-; //i-1段最少要有i-1个数,故从i-1开始
for(int j = i; j<=n; j++) //i段最少要有i个数,故从i开始
{
while(head+<tail && getUP(i,q[head+],q[head])<getDOWN(q[head+], q[head])*val[j]) head++;
dp[i][j] = getDP(i,j,q[head]); while(head+<tail && getUP(i,j,q[tail-])*getDOWN(q[tail-],q[tail-])<=
getUP(i,q[tail-],q[tail-])*getDOWN(j,q[tail-])) tail--;
q[tail++] = j;
}
}
printf("Case %d: %d\n", kase, dp[m][n]);
}
}

最新文章

  1. 如何定位Oracle数据库被锁阻塞会话的根源
  2. ccc 模拟重力 正太分布
  3. Java Performance - 如何调查解决 CPU 问题
  4. JMeter源码集成到Eclipse
  5. C#图片读取和保存
  6. 在IDEA上用python来连接集群上的hive
  7. mysql之7xtrabackup
  8. 16进制字符串转换为byte数组
  9. Grub禁用UUID
  10. (数字IC)低功耗设计入门(六)——门级电路低功耗设计优化
  11. Linux进程通信学习总结
  12. 升级WIN10 (9879)后IE无响应的解决办法
  13. 1945 : 卡贩子Carol
  14. python-基础数据类型,集合及深浅copy
  15. 消除2个按钮之间1px细节引起的冲突
  16. IDC Digital Transition Annual Festival(2018.10.19)
  17. Liunx下安装jdk7
  18. WebLogic 11gR1修改jdk版本
  19. 20145206邹京儒Exp6 信息搜集与漏洞扫描
  20. 整合Struts2框架和Spring框架

热门文章

  1. CDOJ_24 八球胜负
  2. POJ1430 Binary Stirling Numbers
  3. GDKOI賽前總結
  4. 千呼万唤始出来:ArchLinux for Espressobin
  5. VC++ ADO 连接 mysql
  6. 大牛blog汇总
  7. 4.php整合Memcached
  8. CrtmpServr 接收Http流程
  9. 函数式编程( Functional)与命令式编程( Imperative)对比
  10. 基于RFS(robot framework selenium)框架模拟POST/GET请求执行自动化接口测试