[HDU]2829 Lawrence 斜率优化

本文介绍了一个基于斜率优化的算法问题,目标是最小化铁路网的战略价值。通过将铁路网视为一系列数值,该问题转化为如何通过有限次攻击来最小化各部分间连接价值的总和。文章详细解释了解决方案,并提供了完整的C++代码实现。

Lawrence

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

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段, 每一段的代价是这一段中两两数的乘积. 让代价最小.

题解:
题目中是说炸m条边, 所以是m+1段…
算是朝花夕拾了吧, 最近在准备斜率优化的讲稿, 决定再做一下以前想做没做的题练练手…这道题跟普通的斜率优化没有什么区别. dp[i][j]表示前i个数分成j段(代码里是炸j条边)的最小代价. 那么:
dp[i][j] = min(dp[k][j-1] + cost[i] - cost[j] - sum[j] * (sum[i] - sum[j])). cost[i]表示1~i的两两之乘之和.
然后就可以斜率优化了…

给自己的TIPS: 普通变量不要搞混, 方程里变量太多的时候要搞清楚每个变量的含义, 不要搞混/写错了啊喂…

#include<stdio.h>
int n, m;
int cost[1005], sum[1005], dp[1005][1005], q[1005]; 
inline int up(int j, int p, int k){
    return dp[j][k - 1] - dp[p][k - 1] + cost[p] - cost[j] + sum[j] * sum[j] - sum[p] * sum[p];
}
inline int down(int j, int p){
    return sum[j] - sum[p];
}
inline void ratio(){
    for(int i = 1; i <= n; ++i)
        dp[i][0] = cost[i];
    for(int j = 1; j <= m; ++j){
        int h = 1, t = 0;
        q[++t] = 0;
        for(int i = 1; i <= n; ++i){
            while(h < t && up(q[h + 1], q[h], j) <= down(q[h + 1], q[h]) * sum[i]) ++h;
            int k = q[h];
            dp[i][j] = dp[k][j - 1] + cost[i] - cost[k] - sum[k] * (sum[i] - sum[k]);
            while(h < t && up(i, q[t], j) * down(q[t], q[t - 1]) <= up(q[t], q[t - 1], j) * down(i, q[t])) --t;
            q[++t] = i;
        }
    }
    printf("%d\n", dp[n][m]);
}
int main(){
    while(~scanf("%d%d", &n, &m), n + m){
        sum[0] = cost[0] = 0;
        for(int i = 1; i <= n; ++i){
            scanf("%d", &sum[i]);
            cost[i] = sum[i - 1] * sum[i] + cost[i - 1];
            sum[i] += sum[i - 1];
        }
        ratio();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值