hdu 2829 Lawrence(四边形不等式优化dp)
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.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int a[];
int sum[];
int d[];
int dp[][]; //前i个点分成j组
int s[][];
int main(){
ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m){
if(!n&&!m) break;
memset(dp,inf,sizeof(dp));
for(int i=;i<=n;i++)
cin>>a[i],sum[i]=sum[i-]+a[i];
for(int i=;i<=n;i++){
d[i]=a[i]*sum[i-]+d[i-];
}
for(int i=;i<=n;i++){
dp[i][]=d[i];
s[i][]=;
}
for(int j=;j<=m+;j++){
s[n+][j]=n;
for(int i=n;i>=j;i--){
for(int k=s[i][j-];k<=s[i+][j];k++){
if(dp[i][j]>dp[k][j-]+d[i]-(sum[i]-sum[k])*sum[k]-d[k]){
dp[i][j]=dp[k][j-]+d[i]-(sum[i]-sum[k])*sum[k]-d[k];
s[i][j]=k;
}
}
}
}
cout<<dp[n][m+]<<endl;
}
return ;
}
最新文章
- ASP.NET Core管道深度剖析(3):管道是如何处理HTTP请求的?
- Eclipse&#183;如何关联Git库文件和添加JUint库
- JavaScript笔记:DOM基础
- Python之几个技巧特点
- 搭建高性能计算环境(四)、应用软件的安装之VASP
- Servlet+Tomcat 界面登录
- SpringTest 使用说明 -构建无污染纯绿色事务测试框架 (记录用)
- 误删除了Oracle的dbf文件后的解决方法
- ajax post data 获取不到数据,注意 content-type的设置
- 从头开始-04.C语言中流程控制
- hbase1.1.4集群搭建
- Docker:macvlan实现容器跨主机通信 [十四]
- 北大poj- 1012
- HiveServer2的WEB UI界面
- mig_7series_v4_0_data_gen_chk
- Python建立多线程任务并获取每个线程返回值
- Derek解读Bytom源码-protobuf生成比原核心代码
- e616. Determining If a Focus Lost Is Temporary or Permanent
- C# 关于out和ref的问题
- git for c#, commit本地,pushserver