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;
} 

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

时间: 2024-10-07 19:42:05

hdu3480二维斜率优化DP的相关文章

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 pro

hdu2829之二维斜率优化DP

T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. His primary targets were the railroads. A highl

hdu3669之二维斜率DP

Cross the Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others) Total Submission(s): 4176    Accepted Submission(s): 748 Problem Description "Across the Great Wall, we can reach every corner in the world!" No

BZOJ 3675 APIO2014 序列切割 斜率优化DP

题意:链接 方法:斜率优化DP 解析:这题BZ的数据我也是跪了,特意去网上找到当年的数据后面二十个最大的点都过了.就是过不了BZ. 看到这道题自己第一发DP是这么推得: 设f[i][j]是第j次分第i个的最大得分. 那么会推出来f[i][j]=max(f[k][j?1]+sum[i k]?sum[1 k?1]或(sum[k i]?sum[i+1 n]))然后我发现这个式子的复杂度非常高暂且不说.就光那个或的讨论就非常费劲. 于是想了想就放弃了这个念头.中规中矩的去想. 依照以往的思路设出状态f[

BZOJ 3675 APIO2014 序列分割 斜率优化DP

题意:链接 方法:斜率优化DP 解析:这题BZ的数据我也是跪了,特意去网上找到当年的数据后面二十个最大的点都过了,就是过不了BZ. 看到这道题自己第一发DP是这么推得: 设f[i][j]是第j次分第i个的最大得分. 那么会推出来f[i][j]=max(f[k][j?1]+sum[i k]?sum[1 k?1]或(sum[k i]?sum[i+1 n]))然后我发现这个式子的复杂度很高暂且不说,就光那个或的讨论就很费劲. 于是想了想就放弃了这个念头,中规中矩的去想. 按照以往的思路设出状态f[i]

【转】斜率优化DP和四边形不等式优化DP整理

当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重循环跑状态 i,一重循环跑 i 的所有子状态)这样的时间复杂度是O(N^2)而 斜率优化或者四边形不等式优化后的DP 可以将时间复杂度缩减到O(N) O(N^2)可以优化到O(N) ,O(N^3)可以优化到O(N^2),依次类推 斜率优化DP和四边形不等式优化DP主要的原理就是利用斜率或者四边形不等式等数学方法 在所有要判断的子状态中迅速做出判断,所以这里的优化其实是省去了枚举

hdu3507,斜率优化dp

斜率优化\(dp\)入门题. \(ProblemLink\) 先从\(n^2\)的\(dp\)开始 设\(S_i=\sum_{i=1}^n a_i\) \(f_i\)为输出前\(i\)个的最小代价. 显然有\(f_i=min(f_j+(S_i-S_j)^2+M)(j<k)\) 考虑对于点i.j比k\((j>k)\)更优当且仅当 \(f_j+(S_i-S_j)^2<f_k+(S_i-S_k)^2\) \(f_j+S_j^2-2S_iS_j<f_k+S_k^2-2S_iS_k\) \(

斜率优化dp(玩具装箱)

斜率优化dp 斜率优化dp的思想是数形结合,将各种决策点反映在平面直角坐标系中,然后通过斜率进行优化 做法首先将这道题的$n^2$的dp 算法写出来然后将其暴力展开如:f(i)=min(f(j)+(s[i]-s[j]+i-j-1-l)^2)令s[i]=s[i]+i,l=l+1原式变为$f(i)=min(f(j)+(s[i]-s[j]-l)^2)暴力展开后是长这样的:f(i)=f(j)+s[i]^2+s[j]^2+l^2-2s[i]s[j]-2s[i]l+2s[j]l我们可以将这个式子的右边转化为

bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)

题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路必须在同一天中走完. Pine希望每一天走的路长度尽可能相近,所以他希望每一天走的路的长度的方差尽可能小. 帮助Pine求出最小方差是多少. 设方差是v,可以证明,v×m^2是一个整数.为了避免精度误差,输出结果时输出v×m^2. In