hdu2829 四边形优化dp
Lawrence
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
 Total Submission(s): 1701    Accepted Submission(s): 737
 
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.
   
  
 
 
4 5 1 2
4 2
4 5 1 2
0 0
2
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 1005
#define inf (__int64)1<<60
__int64 prime[MAXN],dp[MAXN][MAXN],kk[MAXN][MAXN],cost[MAXN][MAXN],val[MAXN][MAXN]; int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(i=1;i<=n;i++)
{
scanf("%I64d",&prime[i]);
}
memset(cost,0,sizeof(cost));
memset(val,0,sizeof(val));
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
val[i][j]=val[i][j-1]+prime[i]*prime[j]; }
for( i=n-1;i>=1;--i)
{
for( j=i+1;j<=n;++j)
{
cost[i][j]=cost[i+1][j]+val[i][j];
//printf("cost %d %d\n",cost[i][j],ff(i,j));
}
}
for(i=0;i<=m;i++)
for(j=0;j<=n;j++)
{
dp[i][j]=inf;
}
for(i=1;i<n;i++)
{
dp[0][i]=cost[1][i];
kk[0][i]=1;
}
for(i=1;i<=m;i++)
{
kk[i][n+1]=n-1;
for(j=n;j>i;j--)
{
for(k=kk[i-1][j];k<=kk[i][j+1];k++)
{
int temp=dp[i-1][k]+cost[k+1][j];
if(temp<dp[i][j])
{
dp[i][j]=temp;
kk[i][j]=k;
}
}
}
}
printf("%I64d\n",dp[m][n]);
}
return 0;
}
hdu2829 四边形优化dp的更多相关文章
- HDU 2829 Lawrence(四边形优化DP O(n^2))
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
 - HDOJ 3516 Tree Construction 四边形优化dp
		
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意: 大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿 ...
 - 四边形优化dp
		
理解: http://blog.renren.com/share/263498909/1064362501 http://www.cnblogs.com/ronaflx/archive/2011/03 ...
 - [NOI2009]诗人小G 四边形优化DP
		
题目传送门 f[i] = min(f[j] + val(i,j); 其中val(i,j) 满足 四边形dp策略. 代码: #include<bits/stdc++.h> using nam ...
 - zoj 2860 四边形优化dp
		
Breaking Strings Time Limit: 2 Seconds Memory Limit: 65536 KB A certain string-processing lan ...
 - HDU3507 Print Article(斜率优化dp)
		
前几天做多校,知道了这世界上存在dp的优化这样的说法,了解了四边形优化dp,所以今天顺带做一道典型的斜率优化,在百度打斜率优化dp,首先弹出来的就是下面这个网址:http://www.cnblogs. ...
 - hdu 2829 Lawrence(四边形不等式优化dp)
		
T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...
 - BZOJ1563/洛谷P1912  诗人小G  【四边形不等式优化dp】
		
题目链接 洛谷P1912[原题,需输出方案] BZOJ1563[无SPJ,只需输出结果] 题解 四边形不等式 什么是四边形不等式? 一个定义域在整数上的函数\(val(i,j)\),满足对\(\for ...
 - HDU  3506  (环形石子合并)区间dp+四边形优化
		
Monkey Party Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Tot ...
 
随机推荐
- C#与数据库的连接的三种方式
			
学习了.net的知识从C#一直到MVC,我一直觉得基础很重要,最近有复习一下数据库连接的三种方式 1 返回结果集的一张表 public static DataTable ExecuteDataTabl ...
 - NOIP2016提高组D1T2 天天爱跑步
			
n<=300000个点的树,每个点有个人于第Ti秒观测,有m<=300000个人于时间0开始从Sj跑到Tj,速度1个点每秒,输出每个点上的人观察到的跑步的人的数量. 前25分:直接模拟每条 ...
 - [bzoj2152]聪聪可可——点分治
			
Brief Descirption 给定一棵带权树,您需要统计路径长度为3的倍数的路径长度 Algorithm Analyse 点分治. 考察经过重心的路径.统计出所有deep,统计即可. Code ...
 - 如何使用SDK在Ubuntu设备(包括仿真器和桌面)上运用应用程序
			
简介 有三种运行通过SDK创建的应用程序的方式:在桌面上,在联网的Ubuntu设备上,以及在仿真器中.这些方式为互补性方式,因为各有优缺点.您首先将了解如何管理SDK的设备类型,以及哪一个类型用于测试 ...
 - Unordered load/store queue
			
A method and processor for providing full load/store queue functionality to an unordered load/store ...
 - vnc无法显示桌面
			
转载 以下是我的正确配置,解决上述问题,附带说明: 修改后的~/.vnc/xstartup #!/bin/sh # Uncomment the following two lines for n ...
 - 如何在Ubuntu 16.04安装的Git【转】
			
转自:https://www.howtoing.com/how-to-install-git-on-ubuntu-16-04/ 介绍 现代软件开发中不可或缺的工具是某种版本控制系统. 版本控制系统允许 ...
 - HTTP===返回结果的HTTP状态码
			
HTTP 状态码负责表示客户端 HTTP 请求的返回结果.标记服务器端的处理是否正常.通知出现的错误等工作. 1.状态码告知从服务器端返回的请求结果 状态码的职责是当客户端向服务器端发送请求时,描述返 ...
 - letsencrypt的证书转换上传到360网站防护方法
			
命令:openssl 首先letsencrypt生成的证书在 letsencrypt/live/xxx.com/ 下,需要使用cert.pem和privkey.pem文件. 生成crt文件: open ...
 - Grunt环境搭建及使用 前端必备
			
jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前端开发人员,如果你现在还不知道grunt或者听说过 ...