【DP+四边不等式优化】Division
来源:互联网 发布:ubuntu armnoneeabigcc 编辑:程序博客网 时间:2024/05/13 08:52
Division
http://acm.hdu.edu.cn/showproblem.php?pid=3480
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.
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.
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
23 21 2 44 24 7 10 1
Sample Output
Case 1: 1Case 2: 18
DP的四边不等式优化,看了一个晚上,终于对理论上的有了一点理解。
这题还是参考别人的代码改的。
#include<cstdio>#include<algorithm>using namespace std;#define N 10010#define M 5010int val[N];int dp[M][N];//dp[i][j]代表i堆,前j个数的最优解 int s[M][N];//s[i,j]为dp[i,j]的决策量,即dp[i,j]=dp[i,s[i,j]]+(val[j]-val[s[i,j]+1])^2)//dp[i][j]=min(dp[i-1][k]+(val[j]-val[k+1])^2)int n,m;int fun(int x){ return x*x;}int main(){ int t,ti=1; scanf("%d",&t); for(;t--;) { scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) scanf("%d",&val[i]); sort(val+1,val+1+ n); for(int i=1;i<=n;++i) { dp[1][i]=fun(val[i]-val[1]);//前i个数分为1堆的最优解 s[1][i]=0;//dp[1][i]的最优决策是dp[i][0]+(val[i]-val[1])^2 dp[i][i]=0;//前i个数分为i堆的最优解 } for(int i=2;i<=m;++i) { s[i][n+1]=n-1; for(int j=n;j>i;--j) { dp[i][j]=-1; for(int k=s[i-1][j];k<=s[i][j+1]&&k<j;++k)//四边不等式优化 { int temp=dp[i-1][k]+fun(val[j]-val[k+1]); if(dp[i][j]==-1||dp[i][j]>temp)//保留最优解 { dp[i][j]=temp; s[i][j]=k; } } } } printf("Case %d: %d\n",ti++,dp[m][n]); } return 0;}
- 【DP+四边不等式优化】Division
- HDU 3480 Division【dp四边形不等式优化】
- Hdu3480 Division dp+四边形不等式优化
- 【HDU3480】Division-DP+四边形不等式优化+贪心
- hdu 3480 Division (四边形不等式优化DP)
- 四边形不等式优化DP
- 四边形不等式优化dp
- 四边形不等式dp优化
- 四边形不等式优化dp
- Division - HDU 3480 斜率优化,四边形不等式优化
- 四边形不等式优化dp-简介
- hdu2829Lawrence DP+四边形不等式优化
- hdu 3480 Division (斜率优化||四边形不等式)
- hdu 3480 Division(DP-DP优化)
- pku 1160 四边形不等式优化DP
- dp专辑 U - Lawrence [ 四边形不等式优化]
- ZOJ2860----DP+四边形不等式的优化
- HDU3480-----用四边形不等式优化的DP
- 用IrisSkin2.dll美化界面
- 开心逍遥笔V7.2版
- 关于list倒序的两个小方法
- [iOS学习笔记]超级快的JSONKit
- sqlserver 批量修改表前缀. 自创方法.
- 【DP+四边不等式优化】Division
- 堆栈,堆栈,堆和栈的区别
- C语言中time_t数据类型详细介绍
- 基于mini6410的linux驱动学习总结(三 使用驱动程序)
- Oracle维护常用SQL语句
- Linux下Android开发环境搭建
- Java-Package
- 为什么要写博客
- 第10周-任务3-由点到圆再到圆柱体