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. java新知识系列 二

      1:数据库事务隔离以及事务隔离的级别 数据库事务隔离: 在数据库操作中,为了有效保证并发读取数据的正确性,提出的事务隔离级别:为了解决更新丢失,脏读,不可重读(包括虚读和幻读)等问题在标准SQL规 ...

  2. Asp.net Core应用程序部署为服务

    安装前使用dotnet命令运行下看网站能不能正常运行 1.下载nssm,下载后解压文件 下载地址:https://nssm.cc/usage 2.使用命令行工具进入到nssm的目录: 3.执行服务安装 ...

  3. 云数据库PolarDB(一)

    一.出现的背景及PolarDB简介 阿里云,中国第一家拥有完整云计算能力的企业. 2015年,在计算界的奥运会Sort Benchmark中,阿里云计算100TB数据排序只用了不到7分钟,把Apach ...

  4. pymsql模块

    老师的博客地址:http://www.cnblogs.com/wupeiqi/articles/5713330.html 通过pymysql 模块可以通过朋友去操作mysql 数据库,首先的在pip上 ...

  5. Springboot整合Ehcache缓存

    Pom.xml导包 <!-- ehcache --> <dependency> <groupId>org.springframework.boot</grou ...

  6. Operation category READ is not supported in state standby

    Namenode 开启HA之后,由于zookeeper异常,出现脑裂现象 执行 $./hdfs haadmin -getServiceState nn1                         ...

  7. C. Songs Compression(简单贪心)

    水题 #include<iostream> #include<algorithm> using namespace std; #define LL long long ; st ...

  8. AI MobileNet

    MobileNet,是针对移动和嵌入式设备的一类高效模型,基于流线型(streamlined)架构,使用深度可分离卷积(depthwise separable convolution)来构建轻量级深度 ...

  9. InheritedWidget

    下面这个示例是InheritedWidgt的一个简单用法: class CounterProvider extends InheritedWidget{//数据之前必须加上final,下面这三个数据都 ...

  10. 使用 xUnit 编写 ASP.NET Core 单元测试

    还记得 .NET Framework 的 ASP.NET WebForm 吗?那个年代如果要在 Web 层做单元测试简直就是灾难啊..NET Core 吸取教训,在设计上考虑到了可测试性,就连 ASP ...