首页 > 其他 > 详细

6053: Lawrence(区间DP+四边形不等式优化)

时间:2019-11-13 09:16:25      阅读:106      评论:0      收藏:0      [点我收藏+]

6053: Lawrence 技术分享图片

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
总提交: 9            测试通过:6

描述

 

 

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. 

 

 

输入

 

 

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.

 

 

输出

 

 

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.

 

 

样例输入

4 1
4 5 1 2
4 2
4 5 1 2
0 0

样例输出

17
2

解题思路: 和山区建小学那题思维差不多+一个四边形不等式优化!

技术分享图片
 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 
 5 inline ll read(){
 6     ll x=0,f=1;
 7     char ch=getchar();
 8     while(ch<0||ch>9){
 9         if(ch==-) f=-1;
10         ch=getchar();
11     }
12     while(ch>=0&&ch<=9){
13         x=(x<<1)+(x<<3)+(ch^48);
14         ch=getchar();
15     }
16     return x*f;
17 }
18 
19 inline void write(ll x){
20     if(x<0){
21         putchar(-);
22         x=-x;
23     }
24     if(x>9){
25         write(x/10);
26     }
27     putchar(x%10+0);
28 }
29 
30 int n,m;
31 const int maxn=1005;
32 const ll INF=0x3f3f3f3f3f3f3f3f;
33 ll w[maxn][maxn],dp[maxn][maxn],pos[maxn][maxn]; //dp[i][j] 代表含义前j个分成i段的最小价值
34 ll sum1[maxn],sum2[maxn],arr[maxn];
35 
36 ll judgc(ll a,ll b){
37     return ((sum1[b]-sum1[a-1])*(sum1[b]-sum1[a-1])-(sum2[b]-sum2[a-1]))/2;
38 }
39 
40 int main(){
41     while(scanf("%d%d",&n,&m),n||m){
42         m++;
43         for(int i=1;i<=n;i++){
44             arr[i]=read();
45             sum1[i]=sum1[i-1]+arr[i];
46             sum2[i]=sum2[i-1]+arr[i]*arr[i];
47         }
48         memset(dp,INF,sizeof(dp));
49         for(int i=1;i<=n;i++) dp[1][i]=judgc(1,i),pos[1][i]=1;
50         for(int i=2;i<=m;i++){//几块
51             pos[i][n+1]=n;    //确定边界 出界时的分割线
52             for(int j=n;j>=i;j--){
53                 for(int k=pos[i-1][j];k<=pos[i][j+1];k++){
54                     if(dp[i][j]>dp[i-1][k]+judgc(k+1,j)){
55                         dp[i][j]=dp[i-1][k]+judgc(k+1,j);
56                         pos[i][j]=k;
57                     }
58                 }
59             }
60         }
61 //        for(int i=1;i<=m;i++){
62 //            for(int j=i;j<=n;j++){
63 //                cout << i << " " << j << " " << dp[i][j] << endl;
64 //            }
65 //        }
66 
67         printf("%lld\n",dp[m][n]);
68     }
69     return 0;
70 }
View Code

 

 

6053: Lawrence(区间DP+四边形不等式优化)

原文:https://www.cnblogs.com/qq-1585047819/p/11846616.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!