码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2829 Lawrence

时间:2015-09-15 10:42:34      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

Lawrence

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2829
64-bit integer IO format: %I64d      Java class name: Main
 
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

Source

 
解题:斜率优化
 
技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1010;
 4 int sum[maxn],cost[maxn],q[maxn],dp[maxn][maxn],n,m;
 5 bool check(int a,int b,int i,int j){
 6    int tmp = dp[a][j-1] - cost[a] + sum[a]*sum[a];
 7    tmp -= dp[b][j-1] - cost[b] + sum[b]*sum[b];
 8    return tmp > sum[i]*(sum[a] - sum[b]);//a 劣于 b
 9 }
10 int up(int a,int b,int j){
11     int y1 = dp[a][j-1] - cost[a] + sum[a]*sum[a];
12     int y2 = dp[b][j-1] - cost[b] + sum[b]*sum[b];
13     return y1 - y2;
14 }
15 int down(int a,int b){
16     return sum[a] - sum[b];
17 }
18 int main(){
19     while(scanf("%d%d",&n,&m),n||m){
20         sum[0] = cost[0] = 0;
21         for(int i = 1; i <= n; ++i){
22             scanf("%d",sum + i);
23             cost[i] = cost[i-1] + sum[i-1]*sum[i];
24             sum[i] += sum[i-1];
25             dp[i][0] = cost[i];
26             dp[i][i-1] = 0;
27         }
28         for(int j = 1; j <= m; ++j){
29             int hd = 0,tl = 0;
30             q[tl++] = j;
31             for(int i = j + 1; i <= n; ++i){
32                 while(hd + 1 < tl && check(q[hd],q[hd+1],i,j)) ++hd;
33                 dp[i][j] = dp[q[hd]][j-1] + cost[i] - cost[q[hd]] - sum[q[hd]]*(sum[i] - sum[q[hd]]);
34                 while(hd + 1 < tl && up(q[tl-1],q[tl-2],j)*down(i,q[tl-1]) >= up(i,q[tl-1],j)*down(q[tl-1],q[tl-2])) --tl;
35                 q[tl++] = i;
36             }
37         }
38         printf("%d\n",dp[n][m]);
39     }
40     return 0;
41 }
View Code

 

HDU 2829 Lawrence

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4809428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!