Division - HDU 3480 斜率优化,四边形不等式优化

来源:互联网 发布:昆明java语言培训 编辑:程序博客网 时间:2024/05/16 09:19

Division

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


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
23 21 2 44 24 7 10 1
 

Sample Output
Case 1: 1Case 2: 18

题意:将n个数分成m组,每组的权值是最大值减去最小值的平方,求所有组的权值和的最小值。

思路:首先应该将数字进行排序。然后我们来找一下斜率。

设dp[i][j]表示前j个数字分成i组的最小权值和。那么它由dp[i-1][k1],dp[i-1][k2],k1<k2转移过来,就判断应该选择前者还是后者。

如果要选择后者的话,那么dp[i-1][k1]+(num[j]-num[k1+1])^2>=dp[i-1][k2]+(num[j]-num[k2+1])^2

dp[i-1][k1]-2*num[j]*num[k1+1]+num[k1+1]^2>=dp[i-1][k2]-2*num[j]*num[k2+1]+num[k2+1]^2

2*num[j]*(num[k2+1]-num[k1+1])>=dp[i-1][k2]+num[k2+1]^2-dp[i-1][k1]-num[k1+1]^2;

由此我们得到了这个斜率,当num[j]越大时,我们就选择越后面的点,所以这道题可以用斜率优化。

斜率优化代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int T,t,n,m;int dp[5010][10010],a[10010],q[10010],head,tail;void DP(){    int i,j,k,p1,p2,p3,x1,x2,x3,y1,y2,y3;    for(i=1;i<=n;i++)       dp[1][i]=(a[i]-a[1])*(a[i]-a[1]);    for(i=2;i<=m;i++)    {        head=tail=0;        q[tail]=i-1;        for(j=i;j<=n;j++)        {            while(head<tail)            {                p1=q[head];                p2=q[head+1];                x1=a[p1+1];                x2=a[p2+1];                y1=dp[i-1][p1]+x1*x1;                y2=dp[i-1][p2]+x2*x2;                if((y2-y1)<=2*a[j]*(x2-x1))                  head++;                else                  break;            }            k=q[head];            dp[i][j]=dp[i-1][k]+(a[j]-a[k+1])*(a[j]-a[k+1]);            while(head<tail)            {                p1=q[tail-1];p2=q[tail];p3=j;                x1=a[p1+1];x2=a[p2+1];x3=a[p3+1];                y1=dp[i-1][p1]+x1*x1;                y2=dp[i-1][p2]+x2*x2;                y3=dp[i-1][p3]+x3*x3;                if((y2-y1)*(x3-x2)>=(y3-y2)*(x2-x1))                  tail--;                else                  break;            }            q[++tail]=j;        }    }}int main(){    int i,j,k;    scanf("%d",&T);    for(t=1;t<=T;t++)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++)           scanf("%d",&a[i]);        sort(a+1,a+1+n);        DP();        printf("Case %d: %d\n",t,dp[m][n]);    }}

另外此题还可以用四边形不等式优化。

四边形不等式代码如下:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;int T,t,n,m;int dp[5010][10010],s[5010][10010],num[10010],INF=2*1e9;int main(){    int i,j,k,ret;    scanf("%d",&T);    for(t=1;t<=T;t++)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++)           scanf("%d",&num[i]);        sort(num+1,num+1+n);        for(i=1;i<=n;i++)           dp[1][i]=(num[i]-num[1])*(num[i]-num[1]);        for(i=2;i<=m;i++)        {            s[i][n+1]=n;            for(j=n;j>i;j--)            {                dp[i][j]=INF;                for(k=s[i-1][j];k<=s[i][j+1];k++)                {                    ret=dp[i-1][k]+(num[j]-num[k+1])*(num[j]-num[k+1]);                    if(ret<dp[i][j])                    {                        dp[i][j]=ret;                        s[i][j]=k;                    }                }            }        }        printf("Case %d: %d\n",t,dp[m][n]);    }}



0 0
原创粉丝点击