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的更多相关文章

  1. HDU 2829 Lawrence(四边形优化DP O(n^2))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...

  2. HDOJ 3516 Tree Construction 四边形优化dp

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意: 大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿 ...

  3. hdu2829 四边形优化dp

    Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. 四边形优化dp

    理解: http://blog.renren.com/share/263498909/1064362501 http://www.cnblogs.com/ronaflx/archive/2011/03 ...

  5. [NOI2009]诗人小G 四边形优化DP

    题目传送门 f[i] = min(f[j] + val(i,j); 其中val(i,j) 满足 四边形dp策略. 代码: #include<bits/stdc++.h> using nam ...

  6. HDU3507 Print Article(斜率优化dp)

    前几天做多校,知道了这世界上存在dp的优化这样的说法,了解了四边形优化dp,所以今天顺带做一道典型的斜率优化,在百度打斜率优化dp,首先弹出来的就是下面这个网址:http://www.cnblogs. ...

  7. hdu 2829 Lawrence(四边形不等式优化dp)

    T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...

  8. BZOJ1563/洛谷P1912 诗人小G 【四边形不等式优化dp】

    题目链接 洛谷P1912[原题,需输出方案] BZOJ1563[无SPJ,只需输出结果] 题解 四边形不等式 什么是四边形不等式? 一个定义域在整数上的函数\(val(i,j)\),满足对\(\for ...

  9. HDU 3506 (环形石子合并)区间dp+四边形优化

    Monkey Party Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Tot ...

随机推荐

  1. thinkphp中常用的模板变量

    在thinkphp中的模板要加载静态文件如css,js等文件时要经常用到模板常量. 假如项目放在/web/shop中,则如下所示对应常量的输出值: 1 2 3 4 5 6 7 8 9 // 不含域名 ...

  2. Dijkstra算法---HDU 2544 水题(模板)

    /* 对于只会弗洛伊德的我,迪杰斯特拉有点不是很理解,后来发现这主要用于单源最短路,稍稍明白了点,不过还是很菜,这里只是用了邻接矩阵 套模板,对于邻接表暂时还,,,没做题,后续再更新.现将这题贴上,应 ...

  3. ES6-const注意

    注意要点 const 如何做到变量在声明初始化之后不允许改变的?其实 const 其实保证的不是变量的值不变,而是保证变量指向的内存地址所保存的数据不允许改动.此时,你可能已经想到,简单类型和复合类型 ...

  4. Initializing the FallBack certificate failed . TDSSNIClient initialization failed

    安装SQL后服务不能启动,报错: 2014-03-24 14:33:10.06 spid13s     Error: 17190, Severity: 16, State: 1.2014-03-24 ...

  5. SPOJ8791 DYNALCA LCT

    考虑\(LCT\) 不难发现,我们不需要换根... 对于操作\(1\),\(splay(u)\)然后连虚边即可 对于操作\(3\),我们可以先\(access(u)\),然后再\(access(v)\ ...

  6. SB!SB!SB! ----WriteUp

    原题 下载图片 http://ctf5.shiyanbar.com/stega/ste.png 用Stegsolve查看 发现有个二维码 扫码可以知道flag

  7. 协议栈中使用crc校验函数

    CRC校验介绍:循环冗余校验码,原理是多项式除法 ZigBee协议栈:能够使zigbee节点相互之间组网,数据传输,数据获取,数据显示 思路以及步骤: 1.因为IAR的程序是用c写的,所以上网查找如何 ...

  8. linux下mysql自动备份脚本

    脚本放在 /home/user/mysql_backup.sh crontab # crontab -l # m h  dom mon dow   command 28 16 * * * /home/ ...

  9. How To Backup Your Android Phone’s Boot, Recovery And System Partition Images -- RomDump

    One can’t stress enough on the importance of backups and when it comes to tinkering with your Androi ...

  10. JTAG Level Translation

    http://www.freelabs.com/~whitis/electronics/jtag/ One of the big issues in making a JTAG pod is leve ...