hdu2829 Lawrence 斜率优化DP

Lawrence

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


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

     题意基本就是N个点一条链,每个点有个权值,要截断M个地方,算一个最小值是多少。这个值的算法是截断后每段任意两个点权值相乘求和。

     貌似也能用四边形不等式优化做,我是用斜率优化做的,到时再用四边形不等式做一下。

     设sum[i]是前i个点权值和,dp[i][j]是前i个截断j次的最小值,dp[i][j]=min(dp[k][j-1]+a[k+1]*(sum[i]-sum[k+1])+a[k+2]*(sum[i]-sum[k+2])+...+a[i-1]*(sum[i]-sum[i-1])),(1<=k<i),设sum2[i]=sum[i]*a[i],dp[i][j]化简就是min(dp[k][j-1]+(sum[i-1]-sum[k])*sum[i]-(sum2[i-1]-sum2[k]))。对于两个位置k1和k2,k2>k1,这时dp[k2][j-1]+(sum[i-1]-sum[k2])*sum[i]-(sum2[i-1]-sum2[k2])<dp[k1][j-1]+(sum[i-1]-sum[k1])*sum[i]-(sum2[i-1]-sum2[k1])时k2比k1优,化简移项得(sum2[k2]+dp[k2][j-1]-(sum2[k1]+dp[k1][j-1]))/(sum[k2]-sum[k1])<sum[i],那么可以设y=sum2[k]+dp[k][j-1],x=sum[k]。由于sum是逐渐递增的,所以维护一个下凸曲线(斜率逐渐增大),每次从front找不超过sum[i]的最大斜率,由这个点得到当前dp。复杂度O(NM)。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;

const LL MAXN=1010;
const LL MAXM=5010;
const LL INF=0x3f3f3f3f;

LL N,M;
LL sum[MAXN],sum2[MAXN];
LL dp[MAXN][MAXN],q[MAXN],a[MAXN];

LL up(LL i,LL j,LL k){
    return sum2[j]+dp[j][k-1]-(sum2[i]+dp[i][k-1]);
}

LL down(LL i,LL j){
    return sum[j]-sum[i];
}

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%I64d%I64d",&N,&M)!=EOF&&(N||M)){
        for(LL i=1;i<=N;i++) scanf("%I64d",&a[i]);
        sum[0]=0;
        for(LL i=1;i<=N;i++) sum[i]=sum[i-1]+a[i];
        sum2[0]=0;
        for(LL i=1;i<=N;i++) sum2[i]=sum2[i-1]+a[i]*sum[i];
        dp[0][0]=0;
        for(LL i=1;i<=N;i++) dp[i][0]=dp[i-1][0]+a[i]*sum[i-1];
        for(LL j=1;j<=M;j++){
            LL front=0,rear=-1;
            for(LL i=1;i<=N;i++){
                while(front<rear&&up(q[front],q[front+1],j)<=sum[i]*down(q[front],q[front+1])) front++;

                if(front<=rear) dp[i][j]=dp[q[front]][j-1]+(sum[i-1]-sum[q[front]])*sum[i]-(sum2[i-1]-sum2[q[front]]);
                else dp[i][j]=0;
                while(front<rear&&up(q[rear-1],q[rear],j)*down(q[rear],i)>=up(q[rear],i,j)*down(q[rear-1],q[rear])) rear--;
                q[++rear]=i;
            }
        }
        printf("%I64d\n",dp[N][M]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值