hdu 3480 Division(斜率优化DP)

Division

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


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

Source
2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU
 

Recommend
zhengfeng
题意:
给你一个含n个元素集合。要你把它分成m个子集。要使它们价值最小。价值即每个集合中的最大元素减最小元素的平方求和。
思路:
为了使连续区间最大值减最小值最小所以先从大到小排序。然后就很好得到转移方程了。
dp[i][j]代表前i个元素分成j个子集的最小价值。1<=j<=i。
dp[i][j]=MIN(dp[k][j-1]+(a[i]-a[k+1])^2)。1<=k<i且k>=j-1。直接枚举k肯定是要超时的,开始想到了用单调队列优化。结果发现转移方程中有a[i]所以结果与a[i]有关单调队列肯定是不行了。于是想能不能用斜率优化或者四边形不等式什么的。
于是耐着性子把方程展开:
dp[i][j]=MIN(dp[k][j-1]+a[i]^2+a[k+1]^2-2*a[i]*a[k+1])。
设r>k。
设f(k)=dp[k][j-1]+a[i]^2+a[k+1]^2-2*a[i]*a[k+1]。f(r)-f(k)=dp[r][j-1]+a[r+1]^2-a[k+1]^2-2*a[i]*(a[r+1]-a[k+1])
令f(r)-f(k)<=0
y=dp[x][j-1]+a[x+1]^2
x=a[x+1]。
那么
(y(r)-y(k))/(xr-xk)<=2*a[i]。
y与x就与a[i]无关了。可以使用斜率优化斜率小于2*a[i]说明r更优。
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define positive(a) ((a)>0?(a):-(a))
using namespace std;

int a[10010],dp[10010][5010];//a存数值。dp[i][j]表示前i个元素分成j个子集的最小价值
int n,m,head,tail;
int q[10010];//队列
int main()
{
    int t,cas,i,j,k,x1,x2,x3,y1,y2,y3,p1,p2,p3;

    scanf("%d",&t);
    for(cas=1;cas<=t;cas++)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
            scanf("%d",a+i);
        sort(a+1,a+n+1);
        for(i=1;i<=n;i++)
        {
            dp[i][1]=(a[i]-a[1])*(a[i]-a[1]);//一个子集即自己本身
            dp[i][i]=0;//全为单元集价值为0
        }
        for(j=2;j<=m;j++)
        {
            head=tail=0;
            q[tail++]=j-1;//k的范围为j-1<=k<i所以先入队两个元素
            q[tail++]=j;
            for(i=j+1;i<=n;i++)
            {
                while(tail-head>=2)
                {
                    p1=q[head];
                    p2=q[head+1];
                    y1=dp[p1][j-1]+a[p1+1]*a[p1+1];
                    x1=a[p1+1];
                    y2=dp[p2][j-1]+a[p2+1]*a[p2+1];
                    x2=a[p2+1];
                    if(y2-y1<=2*a[i]*(x2-x1))//(y2-y1)/(x2-x1)<=2*a[i]说明y2更优所以去掉y1
                        head++;
                    else
                        break;
                }
                k=q[head];
                dp[i][j]=dp[k][j-1]+(a[i]-a[k+1])*(a[i]-a[k+1]);
                while(tail-head>=2)//加入i去掉上凸点
                {
                    p1=q[tail-2];
                    p2=q[tail-1];
                    p3=i;
                    y3=dp[p3][j-1]+a[p3+1]*a[p3+1];
                    x3=a[p3+1];
                    y2=dp[p2][j-1]+a[p2+1]*a[p2+1];
                    x2=a[p2+1];
                    y1=dp[p1][j-1]+a[p1+1]*a[p1+1];
                    x1=a[p1+1];
                    if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2))
                        tail--;
                    else
                        break;
                }
                q[tail++]=i;
            }
        }
        printf("Case %d: %d\n",cas,dp[n][m]);
    }
    return 0;
}
 

你可能感兴趣的:(c,算法,ACM)