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]=min(dp[i][j],dp[k][j-1]+(a[i]-a[k+1])^2) 这里我们可以用四边形不等式优化(打表可知)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int dp[][]; //dp[i][j] 表示前i个人 分组成j组
int s[][]; //决策数组
int a[];
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
int w=;
while(t--){
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>a[i];
}
sort(a+,a++n); //排序后满足一个区间内的值是首尾的差平方
for(int i=;i<=n;i++){ //初始化边界
dp[i][]=(a[i]-a[])*(a[i]-a[]); //前i个人分成1组
s[i][]=; //初始化决策数组的左边界
}
for(int j=;j<=m;j++){
s[n+][j]=n-; //初始化决策数组的右边界
for(int i=n;i>=j;i--){
dp[i][j]=inf;
for(int k=s[i][j-];k<=s[i+][j];k++){ //四边形不等式优化
if(dp[i][j]>dp[k][j-]+(a[i]-a[k+])*(a[i]-a[k+])){
dp[i][j]=dp[k][j-]+(a[i]-a[k+])*(a[i]-a[k+]);
s[i][j]=k;
}
}
}
}
cout<<"Case "<<++w<<": ";
cout<<dp[n][m]<<endl;
}
return ;
}

hdu 3480 Division(四边形不等式优化)的更多相关文章

  1. 【无聊放个模板系列】HDU 3506 (四边形不等式优化DP-经典石子合并问题[环形])

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  2. hdu 2829 Lawrence(四边形不等式优化dp)

    T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...

  3. HDU 3516 DP 四边形不等式优化 Tree Construction

    设d(i, j)为连通第i个点到第j个点的树的最小长度,则有状态转移方程: d(i, j) = min{ d(i, k) + d(k + 1, j) + p[k].y - p[j].y + p[k+1 ...

  4. HDU 3480 Division(斜率优化+二维DP)

    Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Tota ...

  5. HDU 3480 Division DP斜率优化

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

  6. HDU 3506 DP 四边形不等式优化 Monkey Party

    环形石子合并问题. 有一种方法是取模,而如果空间允许的话(或者滚动数组),可以把长度为n个换拓展成长为2n-1的直线. #include <iostream> #include <c ...

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

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

  8. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  9. HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

    题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...

随机推荐

  1. Bitmap上下合成图片

    合成两张图片,上下叠加的效果: /** * 把两个位图覆盖合成为一个位图,以底层位图的长宽为基准 * * @param backBitmap 在底部的位图 * @param frontBitmap 盖 ...

  2. C# 一般处理程序ashx接收服务端post过来json数据

    这个和前端js的接收方式有点不一样,前端接收用request.form["xxx"]即可

  3. SQLServer之创建表值函数

    表值函数创建注意事项 用户定义表值函数返回 table 数据类型. 对于内联表值函数,没有函数主体,表是单个 SELECT 语句的结果集. 表值函数主要用于数据计算出来返回结果集. 使用SSMS数据库 ...

  4. C#中的?和??,null和Nullable

    from : https://www.cnblogs.com/appleyrx520/p/7018610.html C#单问号(?)与双问号(??)   1.单问号(?) 1.1 单问号运算符可以表示 ...

  5. C#中FormsAuthentication用法实例

    ....本文纯属抄袭....   using System; using System.Web; using System.Web.Security;   namespace AuthTest {   ...

  6. Ubuntu 把最小化、最大化和关闭按钮放到右边

    1.按下"Ctrl+Alt+T"或者"ALT+F2"调出终端 2.输入"gconf-editor",回车.如果未安装gconf-editor ...

  7. 用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。

    数据库中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列. 方法一: select (case when a>b then a el ...

  8. Enterprise architect 类图加时序图

    原文地址:https://segmentfault.com/a/1190000005639047#articleHeader2 新建一个Project 没什么好说的,“文件-新建项目”,然后选择保存位 ...

  9. USING KERBEROS

    https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/managing_smart_cards/u ...

  10. 查看jar包的jdk版本