码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 3480 Division

时间:2014-08-18 14:31:54      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   java   os   io   strong   


斜率优化DP。。。。

对数组排序后,dp【i】【j】表示对前j个物品分i段的最少代价,dp【i】【j】= min{ dp【i-1】【k】+(a【k+1】-a【j】)^2 }复杂度m*n^2      斜率优化一下就可以了。

Division

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


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

bubuko.com,布布扣


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
Hint
The answer will fit into a 32-bit signed integer.
 

Source
 




#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=11000;

int n,m;
int dp[maxn/2][maxn],a[maxn];
int q[maxn],head,tail;

int main()
{
	int T_T,cas=1;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			scanf("%d",a+i);
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++)
			dp[1][i]=(a[i]-a[1])*(a[i]-a[1]);
		for(int i=2;i<=m;i++)
		{
		    head=tail=0;
		    q[tail++]=i-1;
		    for(int j=i;j<=n;j++)
            {
                while(head+1<tail)
                {
                    int p1=q[head];
                    int p2=q[head+1];
                    int x1=a[p1+1],x2=a[p2+1];
                    int y1=dp[i-1][p1]+x1*x1;
                    int y2=dp[i-1][p2]+x2*x2;
                    if((y2-y1)<=(x2-x1)*2*a[j]) head++;
                    else break;
                }
                int k=q[head];
                dp[i][j]=dp[i-1][k]+(a[k+1]-a[j])*(a[k+1]-a[j]);
                while(head+1<tail)
                {
                    int p1=q[tail-2],p2=q[tail-1],p3=j;
                    int x1=a[p1+1],x2=a[p2+1],x3=a[p3+1];
                    int y1=dp[i-1][p1]+x1*x1;
                    int y2=dp[i-1][p2]+x2*x2;
                    int y3=dp[i-1][p3]+x3*x3;
                    if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2)) tail--;
                    else break;
                }
                q[tail++]=j;
            }
		}
		printf("Case %d: %d\n",cas++,dp[m][n]);
    }
	return 0;
}


HDOJ 3480 Division,布布扣,bubuko.com

HDOJ 3480 Division

标签:des   style   http   color   java   os   io   strong   

原文地址:http://blog.csdn.net/ck_boss/article/details/38659509

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!