Lawrence (斜率dp)

Lawrence

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


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
 


/*
 * 用dp[i][x]表示前i个点,炸掉x条边可以破坏的最大值
 * 答案就是tol-dp[n][m]
 * dp[i][x]=max{dp[j][x-1]+sum[j]*(sum[i]-sum[j])}  x-1<j<i
 * 假设在计算i时,k<j,j比k点优
 * dp[k][x-1]+sum[k]*(sum[i]-sum[k])<=dp[j][x-1]+sum[j]*(sum[i]-sum[j])
 * 化简得 ( (sum[j]*sum[j]-dp[j][x-1])-(sum[k]*sum[k]-dp[k][x-1]) ) /(sum[j]-sum[k]  <=sum[i]
 *
 * yj=sum[j]*sum[j]-dp[j][x-1]    xj=sum[j]
 * (yj-yk)/(xj-xk)<=sum[i]
 * 右边不等式是递增的
 * g[k,j]=(yj-yk)/(xj-xk)
 * 上述不等式成立说明j比k优
 * 如果k<j<i  g[k,j]>g[i,j]那么k可以淘汰
 * 如果g[j,i]<sum[i]  j可以淘汰
 *
 *
 */

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 10010
#define MOD 1000000007
#define LL long long
const int mod = 998244353;

int n,m;

LL sum[1200];
LL a[1200];

int que[1100];
LL dp[1200][1200];

LL get(int pos,int x)
{
    return sum[pos]*sum[pos] - dp[pos][x-1];
}

void solve()
{
    memset(dp,0,sizeof(dp));

    for(int i=1;i<=m;i++)
    {
        int fr=0,re=0;
        que[re++] = i;
        for(int j=i+1;j<=n;j++)
        {
            while(fr+1<re && (get(que[fr+1],i) - get(que[fr],i))<=(sum[j] * (sum[que[fr+1]] -sum[que[fr]]))) fr++;

            dp[j][i] = dp[que[fr]][i-1] + sum[que[fr]]*(sum[j] - sum[que[fr]]);

            while(fr+1<re && (get(que[re-1],i)-get(que[re-2],i)) * (sum[j] - sum[que[re-1]]) >= (get(j,i)-get(que[re-1],i)) * (sum[que[re-1]] - sum[que[re-2]]))
                re--;

            que[re++]=j;
        }
    }
}

//LL getDP(int i,int x,int j)
//{
//    return dp[j][x-1]+sum[j]*(sum[i]-sum[j]);
//}
//LL getUp(int j,int x,int k)
//{
//    return sum[j]*sum[j]-dp[j][x-1]-(sum[k]*sum[k]-dp[k][x-1]);
//}
//LL getDown(int j,int k)
//{
//    return sum[j]-sum[k];
//}
//void solve()
//{
//    memset(dp,0,sizeof(dp));
//    int front,rear;
//    for(int x=1;x<=m;x++)
//    {
//        rear=front=0;
//        q[rear++]=x;
//        for(int i=x+1;i<=n;i++)
//        {
//            while(front+1<rear && getUp(q[front+1],x,q[front])<=sum[i]*getDown(q[front+1],q[front]))
//                front++;
//            dp[i][x]=getDP(i,x,q[front]);
//            while(front+1<rear && getUp(i,x,q[rear-1])*getDown(q[rear-1],q[rear-2])<=getUp(q[rear-1],x,q[rear-2])*getDown(i,q[rear-1]))
//                rear--;
//            q[rear++]=i;
//        }
//    }
//   // printf("%d\n",tol-dp[n][m]);
//}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;

        sum[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&a[i]);
            sum[i] = sum[i-1] + a[i];
        }

        LL tot=0;
        for(int i=2;i<=n;i++)
        {
            tot = tot + sum[i-1] * a[i];
        }

        solve();

        printf("%I64d\n",tot - dp[n][m]);

    }
    return 0;

}









 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值