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. PHP经典算法百钱买小鸡

    遇到一道有趣的题,并计算2种方法的效率,发现如果穷举所有组合竟高达1000000次排列~所以简化到了600次.所以,你的一个条件,或者一个运算,可能会提高几千倍的效率! <?php header ...

  2. 跟厂长学PHP7内核(二):源码分析的环境与工具

    本文主要介绍分析源码的方式,其中包含环境的搭建.分析工具的安装以及源码调试的基本操作. 一.工具清单 PHP7.0.12 GDB CLion 二.源码下载及安装 $ wget http://php.n ...

  3. GPL、BSD和Apache开源许可证

    参考资料 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT) 如何选择开源许可证? - 阮一峰的网络日志 开源许可证教程 - 阮一峰的网络日志 简介 自由软件许可证由FSF(Free ...

  4. Bzoj4710 分特产(容斥原理+组合数)

    题面 Bzoj 题解 考虑容斥原理,所有人都有特产的方案数等于: 至少零个人没有特产\(-\)至少一个人没有特产\(+\)至少两个人有特产\(-...\) 接着考虑其中一种情况怎么求(假设现在至少有\ ...

  5. Java 关于集合框架那点事儿

     1.引入集合框架  采用数组存在的一些缺陷:   1.数组长度固定不变,不能很好地适应元素数量动态变化的情况.   2.可通过数组名.length获取数组的长度,却无法直接获取数组中真实存储的个数. ...

  6. jQuery学习总结2

    六.动画效果 6.1.基本 hide([speed,[fn]])隐藏显示的元素 speed: 三种预定速度之一的字符串("slow","normal", or ...

  7. [BZOJ5292][BJOI2018]治疗之雨(概率DP+高斯消元)

    https://blog.csdn.net/xyz32768/article/details/83217209 不难找到DP方程与辅助DP方程,发现DP方程具有后效性,于是高斯消元即可. 但朴素消元显 ...

  8. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

    VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...

  9. 读书笔记_Effective_C++_条款三十八:通过复合塑模出has-a或者is-implemented-in-terms-of

    如果说public是一种is-a的关系的话,那么复合就是has-a的关系.直观来说,复合就是在一个类中采用其他类的对象作为自身的成员变量,可以举个例子,像下面这样: class Person { pr ...

  10. ROS知识(17)----Actionlib使用的例子

    1.Actionlib原理 英文版:DetailedDescription 翻译版:actionlib的身世之谜 2.Actionlib官方教程 Actionlib是ros的重要部件,对于复杂动作的执 ...