HDU 2829 Lawrence

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

2009 Multi-University Training Contest 2 - Host by TJU

解题:斜率优化

 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 }

时间: 2024-10-10 04:44:04

HDU 2829 Lawrence的相关文章

HDU 2829 Lawrence(动态规划-四边形不等式)

Lawrence 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 target

HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程很容易想出来,dp[i][j] 表示前 j 个数分成 i 组.但是复杂度是三次方的,肯定会超时,就要对其进行优化. 有两种方式,一种是斜率对其进行优化,是一个很简单的斜率优化 dp[i][j] = min{dp[i-1][k] - w[k] + sum[k]*sum[k] - sum[k]*sum[

HDU 2829 Lawrence (斜率DP)

斜率DP 设dp[i][j]表示前i点,炸掉j条边的最小值.j<i dp[i][j]=min{dp[k][j-1]+cost[k+1][i]} 又由得出cost[1][i]=cost[1][k]+cost[k+1][i]+sum[k]*(sum[i]-sum[k]) cost[k+1][i]=cost[1][i]-cost[1][k]-sum[k]*(sum[i]-sum[k]) 代入DP方程 可以得出 y=dp[k][j-1]-cost[1][k]+sum[k]^2 x=sum[k]. 斜率s

【HDU】2829 Lawrence

http://acm.hdu.edu.cn/showproblem.php?pid=2829 题意:将长度为n的序列分成p+1块,使得$\sum_{每块}\sum_{i<j} a[i]a[j]$最小(n<=1000,1<=a[i]<=100) #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <iostream&

【HDU 2829】Lawrence

Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2427    Accepted Submission(s): 1078 Problem Description T. E. Lawrence was a controversial figure during World War I. He was a British

Lawrence HDU - 2829 斜率dp

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MAX = 1000+10; //dp[i][j]:把前 i 个数分成 j 段后能得到的序列的最小权值和 //dp[i][j] = min( dp[k][j-1] + cost[i] - cost[k] - sum[k] *(sum[i] - sum[

HDOJ 2829 Lawrence

四边形不等式优化DP Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2220    Accepted Submission(s): 975 Problem Description T. E. Lawrence was a controversial figure during World War I. He was

hdu 2829

Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2484    Accepted Submission(s): 1105 Problem Description T. E. Lawrence was a controversial figure during World War I. He was a British o

hdu 2829 dp+四边形不等式优化

用w[i][j]表示i到j之间没有边毁掉的费用. 有一种很好证明w[i][j]是否满足四边形不等式的条件. 若(w[i+1][j]-w[i][j])是关于j的减函数,就是满足条件的.可以证明这里的w[i][j]是瞒住条件的. #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <string> #includ