Division

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

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
题意:给你一个容量为n的集合 现在选取 m个子集 并且要求m个子集的并集为原集合  每个集合的代价为集合内(MAX – MIN)^2 求最少的代价
题解:
 #pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define ll __int64
#define mod 1000000007
using namespace std;
int t;
int n,m;
int a[];
int dp[][];
int q[],head,tail;
int main()
{
scanf("%d",&t);
for(int s=; s<=t; s++)
{
scanf("%d %d",&n,&m);
for(int j=; j<=n; j++)
scanf("%d",&a[j]);
sort(a+,a++n);
for(int j=; j<=n; j++)
dp[][j]=(a[j]-a[])*(a[j]-a[]);
for(int i=; i<=m; i++)
{
head=tail=;
q[tail++]=i-;
for(int j=i; j<=n; j++)
{
while(head+<tail)
{
int p1=q[head],p2=q[head+];
int x1=a[p1+],x2=a[p2+];
int y1=dp[i-][p1]+x1*x1,y2=dp[i-][p2]+x2*x2;
if(y2-y1<*a[j]*(x2-x1))
head++;
else
break;
}
int k=q[head];
dp[i][j]=dp[i-][k]+(a[j]-a[k+])*(a[j]-a[k+]);
while(head+<tail&&j!=n)
{
int p1=q[tail-],p2=q[tail-],p3=j;
int x1=a[p1+],x2=a[p2+],x3=a[p3+];
int y1=dp[i-][p1]+x1*x1,y2=dp[i-][p2]+x2*x2,y3=dp[i-][p3]+x3*x3;
if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2))
tail--;
else
break;
}
q[tail++]=j;
}
}
printf("Case %d: %d\n",s,dp[m][n]);
}
return ;
}

HDU 3480 斜率dp的更多相关文章

  1. B - Lawrence HDU - 2829 斜率dp dp转移方程不好写

    B - Lawrence HDU - 2829 这个题目我觉得很难,难在这个dp方程不会写. 看了网上的题解,看了很久才理解这个dp转移方程 dp[i][j] 表示前面1~j 位并且以 j 结尾分成了 ...

  2. hdu 3507 斜率dp

    不好理解,先多做几个再看 此题是很基础的斜率DP的入门题. 题意很清楚,就是输出序列a[n],每连续输出的费用是连续输出的数字和的平方加上常数M 让我们求这个费用的最小值. 设dp[i]表示输出前i个 ...

  3. D - Pearls HDU - 1300 斜率dp+二分

    D - Pearls HDU - 1300 这个题目也是一个比较裸的斜率dp,依照之前可以推一下这个公式,这个很好推 这个注意题目已经按照价格升序排列序,所以还是前缀和还是单调的. sum[i] 表示 ...

  4. hdu 2829 斜率DP

    思路:dp[i][x]=dp[j][x-1]+val[i]-val[j]-sum[j]*sum[i]+sum[j]*sum[j]; 其中val[i]表示1~~i是一段的权值. 然后就是普通斜率dp做法 ...

  5. HDU 3480 Division DP斜率优化

    解题思路 第一步显然是将原数组排序嘛--然后分成一些不相交的子集,这样显然最小.重点是怎么分. 首先,我们写出一个最暴力的\(DP\): 我们令$F[ i ][ j ] $ 为到第\(i\)位,分成\ ...

  6. hdu 2993 斜率dp

    思路:直接通过斜率优化进行求解. #include<iostream> #include<cstdio> #include<algorithm> #include& ...

  7. hdu 4258 斜率DP

    思路:dp[i]=dp[j]+(num[i]-num[j+1])^2; #include<iostream> #include<cstring> #include<alg ...

  8. hdu 3480 Division(斜率优化DP)

    题目链接:hdu 3480 Division 题意: 给你一个有n个数的集合S,现在让你选出m个子集合,使这m个子集合并起来为S,并且每个集合的(max-min)2 之和要最小. 题解: 运用贪心的思 ...

  9. HDU 3480 - Division - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

随机推荐

  1. 获取Java线程返回值的几种方式

    在实际开发过程中,我们有时候会遇到主线程调用子线程,要等待子线程返回的结果来进行下一步动作的业务. 那么怎么获取子线程返回的值呢,我这里总结了三种方式: 主线程等待. Join方法等待. 实现Call ...

  2. ADO.Net之SqlConnection、 Sqlcommand的应用

    ADO.Net之SqlConnection. Sqlcommand的应用 SqlConnection 的介绍与应用 1.介绍与作用 SqlConnection是ADO.NET中的连接类. 使用sqlc ...

  3. Professional Books

    Machine Learning:     Pattern Recognition and Machine Learning(PRML)    https://mqshen.gitbooks.io/p ...

  4. app结合unity3D程序中遇到的问题 MapFileParser unity3d导出到IOS程序下 集成unity3dAR功能

    转载自: 来自AR学院(www.arvrschool.com),原文地址为:http://www.arvrschool.com/index.php?c=post&a=modify&ti ...

  5. nginx配置,php安装

    yum -y install libxml2 libxml2-develyum -y install libxslt-devel yum -y install bzip2-devel yum -y i ...

  6. 【每日scrum】NO.8

    (1) 在图的设计过程中掌握了图的基本运算函数的算法的理解和程序的有效吸收,包括图的深度和广度优先的遍历,对图的联通分量的求解,图的最小生成树,图的拓扑排序,图的关键路径, (2)在迪杰斯特拉算法的基 ...

  7. OOP 2.1 类和对象的基本概念2

    1.成员函数的另一种写法:类的成员函数和类的定义分开写 e.g. class rectangle { public: int w,h; int area(); int p(); void init(i ...

  8. ACM 第二十天

    积性函数.杜教筛 练习题 莫比乌斯函数之和 51Nod - 1244 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号. ...

  9. 2nd 四人小组项目的进一步分析

    组长:林莉 组员:王东涵.宫丽君.胡丽娜 项目选题:车辆管理系统(附加相关员工管理) 项目期限:暂定十周 一.NABCD模型 N-Need 需求分析及相应功能设置 需求概述: 管理库中车辆信息.相关人 ...

  10. python基础(一)简单入门

    一.第一个python程序 1.交互式编程 直接在命令行里面输入python即可进入python交互式命令行,linux下一样: 在 python 提示符中输入以下文本信息,然后按 Enter 键查看 ...