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 ...
随机推荐
- vue.js 是如何做到数据响应的
许多前端JavaScript框架(例如Angular,React和Vue)都有自己的数据相应引擎.通过了解相应性及其工作原理,您可以提高开发技能并更有效地使用JavaScript框架.在视频和下面的文 ...
- iOS 9应用开发教程之ios9的视图
iOS 9应用开发教程之ios9的视图 了解IOS9的视图 在iPhone或者iPad中,用户看到的和摸到的都是视图.视图是用户界面的重要组成元素.本节将主要讲解ios9视图的添加.删除以及位置和大小 ...
- 深入理解yield
yield的英文单词意思是生产,刚接触Python的时候感到非常困惑,一直没弄明白yield的用法. 只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: 1 2 3 def ad ...
- BZOJ 1951SDOI2010 古代猪文
真是到很强的数学题 先利用欧拉定理A^B %p=A^(B%φ(p)+φ(p) ) %p 然后利用卢卡斯定理求出在modφ(p)的几个约数下的解 再利用中国剩余定理合并 计算答案即可 By:大奕哥 #i ...
- luogu4595 [COCI2011-2012#5] POPLOCAVANJE 后缀自动机
看着就像后缀自动机.... 然后搜了一下,网上一大把的\(AC\)自动机 嗯...... 不管了,打一个试试 然后就过了\(QAQ\) 我们考虑对于每个点\(i\)求出它往前最长能匹配的子串的长度 可 ...
- BZOJ.2916.[POI1997]Monochromatic Triangles(三元环)
题目链接 \(Description\) n个点的完全图,其中有m条边用红边相连,其余边为蓝色.求其中三边同色的三角形个数. \(Solution\) 直接求同色 除了n^3 不会.. 三角形总数是C ...
- bzoj1503 Splay 维护名次数,支持删除
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题解: 维护一颗Splay和一个外部变量,树中每个节点表示一个人,节点权值a + 外部变 ...
- codecombat js
#1 // Move to the gem. // Don't touch the walls! // Type your code below. this.moveRight(); this.mov ...
- python—第三库的安装方法
Windows系统下安装第三方Python库的三种方法: 1.使用easy_install命令安装 一般在安装完Python后再C:\Python27\Scripts 目录下有 easy_instal ...
- Git_撤销修改
自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行: $ cat readme.txt Git is a distributed version co ...