zoj 2860 四边形优化dp
Breaking Strings
Time Limit: 2 Seconds Memory Limit: 65536 KB
A certain string-processing language allows the programmer to break a string into two pieces. Since this involves copying the old string, it costs n units of time to break a string of n characters into two pieces. Suppose a programmer wants to break a string into many pieces. The order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20 character string after characters 3, 8, and 10 (numbering the characters in ascending order from the left-hand end, starting from 1). If the breaks are made in left-to-right order, then the first break cost 20 units of time, the second break costs 17 units of time, and the third breaks costs 12 units of time, a total of 49 units of time (see the sample below). If the breaks are made in right-to-left order, then the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, a total of 38 units of time.
The cost of making the breaks in left-to-right order:
thisisastringofchars (original)
thi sisastringofchars (cost:20 units)
thi sisas tringofchars (cost:17 units)
thi sisas tr ingofchars (cost:12 units)
Total: 49 units.
The cost of making the breaks in right-to-left order:
thisisastringofchars (original)
thisisastr ingofchars (cost:20 units)
thisisas tr ingofchars (cost:10 units)
thi sisas tr ingofchars (cost: 8 units)
Total: 38 units.
Input:
There are several test cases! In each test case, the first line contains 2 integers N (2<=N<=10000000) and M (1<=M<=1000, M<N). N is the original length of the string, and M is the number of the breaks. The following lines contain M integers Mi (1<=Mi<N) in ascending order that represent the breaking positions from the string's left-hand end.
Output:
For each test case, output in one line the least cost to make all the breakings.
Sample Input:
20 3
3 8 10
Sample Output:
37
Author: Wei, Qizheng
Source: ZOJ Monthly, June 200
和那个石子合并成集合竟然是一模一样,字符串就是把整个分成一个个,也可以看成一个个小石子合成一个大的集合!这其实是一样的!所以,要做的就是把整个分成一个个小的块,再合并起来就得到了答案,核心是一样的!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 1050
#define inf 1000000000
int prime[MAXN];
int dp[MAXN][MAXN],kk[MAXN][MAXN],sum[MAXN],p[MAXN];
int main()
{
int n,i,k,j,len,m;
while(scanf("%d%d",&m,&n)!=EOF)
{
p[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&p[i]);
prime[i]=p[i]-p[i-1];
sum[i]=sum[i-1]+prime[i];
}
sum[0]=0;
n++;
prime[n]=m-p[n-1];
sum[n]=sum[n-1]+prime[n];
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
dp[i][j]=inf;
}
for(i=1;i<=n;i++)
{
dp[i][i]=0;
kk[i][i]=i;
}
for(len=2;len<=n;len++)
{ for(i=1;i<=n-len+1;i++)
{
j=i+len-1; for(k=kk[i][j-1];k<=kk[i+1][j];k++)//此时的k取法不同
{
int temp=dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1];
if(temp<dp[i][j])
{
dp[i][j]=temp;
kk[i][j]=k;
}
}
}
}
printf("%d\n",dp[1][n]); }
return 0;
}
zoj 2860 四边形优化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 题意: 大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿 ...
- hdu2829 四边形优化dp
Lawrence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 四边形优化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 ...
- 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 ...
随机推荐
- CentOS 配置自启动Redis
第一步: 在/etc/init.d/目录下建立一个名字为 redis 的启动脚本 cd /etc/init.d touch redis 然后在这个脚本中添加如下脚本 <注意修改自己的PIDFI ...
- 【知了堂学习笔记】java 编写几种常见排序算法
排序的分类: 一.交换排序 所谓交换,就是根据序列中两个记录键值的比较结果来对换这两个记录在序列中的位置,交换排序的特点是:将键值较大的记录向序列的尾部移动,键值较小的记录向序列的前部移动. 1.冒泡 ...
- Mermaid 学习
基础 在 VS code 中安装插件 Markdown Preview Mermaid Support,则便可支持 Mermaid 流程图 flowchart graph LR; A-->B; ...
- OpenGL 笔记<3> 数据传递 一
Sending data to a shader using vertex attributes and vertex buffer object 上次我们说到着色器的编译和连接,后面的事情没有做过多 ...
- Xshell拖拽上传文件插件
lrzsz是一款在linux里可代替ftp上传和下载的程序.在linux中支持直接拖拽上传的插件:同时也支持rz和sz进行命令上传和下载. 插件安装 yum -y install lrzsz 上传(r ...
- openstack policy 鉴权过程分析
转:http://blog.chinaunix.net/uid-20940095-id-4144300.html 1. openstack 鉴权简单介绍 众所周知,openstack通过k ...
- python opencv3 人脸识别的例子
一个人脸识别的例子 程序中用到了公共数据集, 欢迎去我的git上下载源码,源码里带有数据集 git:https://github.com/linyi0604/Computer-Vision 脚本中一个 ...
- window.open()/剪切板ZeroClipboard
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- django组件之contenttype(一)
方式1:适用于1张表和另一张表要关联的时候. 1.路飞学成表设计: 2.将2个价格策略表合并1张表. 3.如果再加一张表,那价格策略表的表结构会发生改变. 这样不合理的,我们的表结构一般设计完就不会 ...
- 解决Jboss中log4j在应用里面无法使用的问题
[参考1]解决Jboss中log4j在应用里面无法使用的问题http://developer.51cto.com/art/200906/128691.htm文章中说到“如果你的应用下存在WEB-INF ...