hdu2829 Lawrence 斜率優化 or 四邊形不等式優化

題目鏈接:戳這裏

Lawrence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4665    Accepted Submission(s): 2142


Problem Description
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 highly fictionalized version of his exploits was presented in the blockbuster movie, "Lawrence of Arabia".

You are to write a program to help Lawrence figure out how to best use his limited resources. You have some information from British Intelligence. First, the rail line is completely linear---there are no branches, no spurs. Next, British Intelligence has assigned a Strategic Importance to each depot---an integer from 1 to 100. A depot is of no use on its own, it only has value if it is connected to other depots. The Strategic Value of the entire railroad is calculated by adding up the products of the Strategic Values for every pair of depots that are connected, directly or indirectly, by the rail line. Consider this railroad: 


Its Strategic Value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.

Now, suppose that Lawrence only has enough resources for one attack. He cannot attack the depots themselves---they are too well defended. He must attack the rail line between depots, in the middle of the desert. Consider what would happen if Lawrence attacked this rail line right in the middle: 

The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots: 

The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence's best option.

Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest Strategic Value that he can achieve for that railroad. 
 

Input
There will be several data sets. Each data set will begin with a line with two integers, n and m. n is the number of depots on the railroad (1≤n≤1000), and m is the number of attacks Lawrence has resources for (0≤m<n). On the next line will be n integers, each from 1 to 100, indicating the Strategic Value of each depot in order. End of input will be marked by a line with n=0 and m=0, which should not be processed.
 

Output
For each data set, output a single integer, indicating the smallest Strategic Value for the railroad that Lawrence can achieve with his attacks. Output each integer in its own line.
 

Sample Input
4 1 4 5 1 2 4 2 4 5 1 2 0 0
 

Sample Output
17 2
 

Source
 
題目大意:有n個數要分成m+1段,最終代價是sigma(每段內的數兩兩乘積的和),如果段內只有1個數則該段代價爲0,求最小的最終代價。

題解:DP,但要用斜率優化或者四邊形不等式優化。

先放DP方程:dp[i][j]表示前i個數分成j+1段的最小總代價,cost[i]表示前i個數兩兩相乘的和,sum[i]表示前i個數的和。

則dp[i][j]=min{dp[k][j-1]+cost[i]-cost[k]-sum[k]*(sum[i]-sum[k])}

斜率優化:

設k2<k1且k1比k2優。

可以推出斜率式(dp[k1][j-1]-cost[k1]+sum[k1]*sum[k1]-dp[k2][j-1]+cost[k2]-sum[k2]*sum[k2])/(sum[k1]-sum[k2])<=sum[i]

轉移即可,避免除法是個好習慣。另外還可以滾動數組優化一下空間。

代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int read()
{
    char c;int sum=0,f=1;c=getchar();
    while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0' && c<='9'){sum=sum*10+c-'0';c=getchar();}
    return sum*f;
}
int n,m;
int a[1005];
int dp[1005][1005];
int cost[1005],sum[1005];
int gety(int j,int k1,int k2)
{
    return (dp[k1][j-1]-cost[k1]+sum[k1]*sum[k1]-dp[k2][j-1]+cost[k2]-sum[k2]*sum[k2]);
}
int getx(int k1,int k2)
{
    return sum[k1]-sum[k2];
}
int q[1005],head,tail;
int main()
{
    while(n=read(),m=read(), n||m)
    {
        memset(cost,0,sizeof(cost));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        a[i]=read(),sum[i]=sum[i-1]+a[i];
        for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++) cost[j]+=a[i]*a[j];
        for(int i=1;i<=n;i++) cost[i]+=cost[i-1];
        for(int i=1;i<=n;i++)
        dp[i][0]=cost[i];
        for(int j=1;j<=m;j++)
        {
            head=0,tail=0,q[tail++]=0;
            for(int i=1;i<=n;i++)
            {
                while(head+1<tail && gety(j,q[head+1],q[head])<=sum[i]*getx(q[head+1],q[head]))
                head++;
                int k=q[head];
                dp[i][j]=dp[k][j-1]+cost[i]-cost[k]-sum[k]*(sum[i]-sum[k]);
                while(head+1<tail && gety(j,i,q[tail-1])*getx(q[tail-1],q[tail-2])<=gety(j,q[tail-1],q[tail-2])*getx(i,q[tail-1]))
                tail--;
                q[tail++]=i;
            }
        }
        printf("%d\n",dp[n][m]);
    }
    return 0;
}


發佈了78 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章