HDU-2829 Lawrence(二维斜率优化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 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

题目大意:

给你一段长度为n的序列,让你分成m+1段,每一段的价值为

                                                                               \sum_{i = l}^r\sum_{j = i+1}^r(val[i]*val[j])

让你找出一种分法,使得总和价值最小

解题思路:

dp[i][k]表示从1到i, 总共分k段的最小价值和

sum[i],为前缀和,Sum[i]为从1到i的这段序列的价值。

易得dp[i][k] = dp[j][k-1]+Sum[i]-Sum[j]-(sum[i]-sum[j])*sum[j];

对于a<b,假设从b转移到i优于从a转移到i,则有不等式

                               dp[b][k-1]+Sum[i]-Sum[b]-(sum[i]-sum[b])*sum[b]<dp[a][k-1]+Sum[i]-Sum[a]-(sum[i]-sum[a])*sum[a]

化简

                                                   \frac{(dp[b][k-1]-Sum[b]+sum^2[b])-(dp[a][k-1]-Sum[a]+sum^2[a])}{sum[b]-sum[a]}<sum[i]

推出这个公式之后就使用斜率优化就可以了。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cmath>

using namespace std;
const long long inf = 0x3f3f3f3f;
const int maxn = 1005;

int n, m, k;
long long dp[maxn][maxn],num[maxn],sum[maxn],Sum[maxn];
int q[maxn];

long long getup(int b, int a)
{
    return dp[b][k-1]-Sum[b]+sum[b]*sum[b]-(dp[a][k-1]-Sum[a]+sum[a]*sum[a]);
}
long long getdown(int b, int a)
{
    return sum[b]-sum[a];
}


int main()
{
    while(scanf("%d%d", &n, &m)&&n)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%lld", &num[i]);
            sum[i] = sum[i-1]+num[i];
            Sum[i] = Sum[i-1]+sum[i-1]*num[i];
            dp[i][1] = Sum[i];
        }
        int head,tail;
        for(k = 2; k <= m+1; k++)
        {
            head = tail = 1;
            q[1] = 0;

            for(int i = 1; i <= n; i++)
            {
                while(head<tail&&getup(q[head+1],q[head])<=sum[i]*getdown(q[head+1],q[head])) head++;
                dp[i][k] = dp[q[head]][k-1]+Sum[i]-Sum[q[head]]-(sum[i]-sum[q[head]])*sum[q[head]];
                while(head<tail&&getup(q[tail],q[tail-1])*getdown(i,q[tail])>=getup(i,q[tail])*getdown(q[tail],q[tail-1])) tail--;
                q[++tail] = i;
            }

        }
        printf("%lld\n", dp[n][m+1]);
    }
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值