hdu 2829 斜率优化DP

Lawrence

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

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.

代码:

#include<iostream>  
#include<cstring>  
#include<cstdio>  
#include<algorithm>  
#include<cmath>
#include<vector>  
using namespace std;  
const int N=1010;  
int n,m;  
int a[N],sum[N],w[N],dp[N][N],q[N];  
int dy(int x,int j,int i)  
{  
    return dp[x][i]-w[i]+sum[i]*sum[i]-(dp[x][j]-w[j]+sum[j]*sum[j]);   
}  
int dx(int x,int y)
{
    return sum[y]-sum[x];
}
void DP()  
{  
    int head,tail;
    for (int i=1;i<=n;i++)
    dp[1][i]=w[i];
    for (int i=2;i<=m+1;++i) 
    {  
        head=tail=0;  
        q[tail++]=i-1;  
        for (int j=i;j<=n;++j) 
        {  
            while (tail-head>=2) 
            {  
                int p=q[head],k=q[head+1];  
                if (dy(i-1,p,k)<sum[j]*dx(p,k)) head++;  
                else break;   
            }  
            dp[i][j]=dp[i-1][q[head]]+w[j]-w[q[head]]-sum[q[head]]*(sum[j]-sum[q[head]]);  
            while (tail-head>=2) 
            {  
                int p = q[tail-2], k = q[tail-1];  
                if (dy(i-1,p,k)*dx(k,j)>=dx(p,k)*dy(i-1,k,j)) tail--;  
                else break;  
            }  
            q[tail++]=j;  
        }  
    }  
    printf("%d\n",dp[m+1][n]);   
}  
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if (n==0&&m==0) break;
        sum[0]=0;
        w[0]=0;
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
            w[i]=w[i-1]+sum[i-1]*a[i];
        }
        DP();
    }
    return 0; 
} 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值