hdu3480二维斜率优化DP

解决集合划分问题的斜率优化算法
本文探讨了一个关于集合划分的问题,其中涉及到通过排序和斜率优化算法来求解多个子集的最优划分,使得每个子集的代价之和最小。详细介绍了算法的实现过程,包括使用滚动数组、单调队列维护最值等关键步骤,以及如何通过数学分析确定最优划分。

Division

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


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

/*分析:
首先对于斜率dp我有个总结:
斜率dp一般应用于连续的一段或几段求最值
既1~k,k+1~j,j+1~...这样分段而不能跳开来求
仅仅有连续段才干用单调队列维护最值然后
dp[i]=dp[j]+(j+1~i)的值。 

对于本题:
题目要求m个子数组的最值。而子数组中的元素不一定是原数组连续的
所以肯定不能直接用斜率优化,经过分析能够发现先进行从小到大排序
然后连续的m段最值就是能够求最值了。

所以:先对原数组进行从小到大排序
dp[i][j]表示以i结尾的j段的最值
从k+1~i作为一段 
则:dp[i][j]=dp[k][j-1]+(s[i]-s[k+1])^2
如今就是怎样求到这个k使得dp[i][j]最小
如果k2<=k1<i
若:dp[k1][j-1]+(s[i]-s[k1+1])^2 <= dp[k2][j-1]+(s[i]-s[k2+1])^2
=>dp[k1][j-1]+s[k1+1]^2 - (dp[k2][j-1]+s[k2+1]^2) / (2s[k1+1]-2s[k2+1]) <= s[i]
所以:
y1 = dp[k1][j-1]+s[k1+1]^2
x1 = 2s[k1+1]
y2 = dp[k2][j-1]+s[k2+1]^2
x2 = 2s[k2+1]

=>(y1 - y2)/(x1 - x2) <= i
单调队列维护下凸折线 
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#include <limits.h>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX = 10000+10;
int n,m,index;
int q[MAX];
int s[MAX],dp[2][MAX];//採用滚动数组

int GetY(int k1,int k2){
	return dp[index^1][k1]+s[k1+1]*s[k1+1] - (dp[index^1][k2]+s[k2+1]*s[k2+1]);
}

int GetX(int k1,int k2){
	return 2*(s[k1+1]-s[k2+1]);
}

int DP(){
	int head=0,tail=1;
	index=0;
	for(int i=1;i<=n;++i)dp[index][i]=INF;//初始化
	//dp[index][0]=0;
	for(int i=1;i<=m;++i){
		index=index^1;
		head=tail=0;
		q[tail++]=0;
		for(int j=1;j<=n;++j){
			//dp[index^1][0]=(i-1)*(s[j]-s[1])*(s[j]-s[1]);
			while(head+1<tail && GetY(q[head+1],q[head]) <= GetX(q[head+1],q[head])*s[j])++head;
			while(head+1<tail && GetY(j,q[tail-1])*GetX(q[tail-1],q[tail-2]) <= GetY(q[tail-1],q[tail-2])*GetX(j,q[tail-1]))--tail;
			q[tail++]=j;
			int k=q[head];
			dp[index][j]=dp[index^1][k]+(s[j]-s[k+1])*(s[j]-s[k+1]);
		}
	}
	return dp[index][n];
}

int main(){
	int t,num=0;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;++i)scanf("%d",s+i);
		sort(s+1,s+1+n);
		printf("Case %d: %d\n",++num,DP());
	}
	return 0;
} 


版权声明:本文博主原创文章。博客,未经同意不得转载。

转载于:https://www.cnblogs.com/bhlsheji/p/4877840.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值