poj 3390 Print Words in Lines 动态规划
来源:互联网 发布:我好想你网络歌手 编辑:程序博客网 时间:2024/05/28 03:02
题意:
给n个单词的长度和每行最多能放的字符数m,每行产生的值为(m-x)^2,x是该行的字符数(包括单词之间的空格),求把所有单词放完产生的值的最小和。
分析:
动态规划很重要的就是状态的定义,在由子问题向父问题推进的过程中,定义的状态要能对之前的所有情况进行总结,比如背包问题中dp[i][v]中的v,不管之前1~i-1个物品如何取舍,他们的总重量肯定在0~v之中,故每步能把指数级的问题线性化。这题也是,刚考虑第i个单词时,前面所有单词不管怎么放最后一个的结束位置肯定在1~m之间,故定义dp[i][s](s<=m)表示放完前i个单词第i个单词末位位于该行s处的最小值。
代码:
//poj 3390//sep9#include <iostream>using namespace std;const int maxM=108;const int maxN=10004;int dp[maxN+10][maxM+10];int L[maxN];int main(){int cases;scanf("%d",&cases);while(cases--){int m,n,s;scanf("%d%d",&m,&n);for(int i=1;i<=n;++i)scanf("%d",&L[i]);memset(dp,0x7f,sizeof(dp));dp[0][m]=0;for(int i=1;i<=n;++i){int x=dp[maxN][maxM];for(s=m;s>=0;--s)x=min(x,dp[i-1][s]);dp[i][L[i]]=x+(m-L[i])*(m-L[i]);for(s=L[i]+2;s<=m;++s){int x=s-L[i]-1;if(dp[i-1][x]==dp[maxN][maxM])continue;int y=dp[i-1][x]-(m-x)*(m-x)+(m-s)*(m-s);dp[i][s]=y;}}int ans=dp[0][maxM];for(s=0;s<=m;++s)ans=min(ans,dp[n][s]);printf("%d\n",ans);}return 0;}
0 0
- poj 3390 Print Words in Lines 动态规划
- poj 3390 Print Words in Lines
- POJ 3390 Print Words in Lines(DP)
- POJ 3390 Print Words in Lines(DP)
- UESTC 1214 divide && PKU 3390 Print Words in Lines [DP]
- 【动态规划】print article
- POJ1923Fourier's Lines题解动态规划DP
- HDU1502_Regular Words动态规划+高精度
- [LeetCode] Text Justification (put words in lines with same length)
- HDU_P3507 Print Article(动态规划+斜率优化)
- Wrap persistent words count one line in print program
- POJ 2559 Largest Rectangle in a Histogram -- 动态规划
- POJ 2559 Largest Rectangle in a Histogram -- 动态规划
- [HDU 1502][动态规划]Regular Words
- poj 2411 动态规划
- Poj 1157(动态规划)
- POJ 1651动态规划
- Poj 1221 动态规划
- C++ STL 使用总结
- Boost.Asio基础(五) 异步编程初探
- linux 分析进程占用CPU过高
- Maven 项目不打包 *.hbm.xml 映射文件
- SlidingMenu常用属性设置记录
- poj 3390 Print Words in Lines 动态规划
- Unity3D 里IK手柄的用运
- 两个页面之间Bundle传值
- 正则表达式笔记
- 玩转大数据系列之Apache Pig高级技能之函数编程(六)
- 挑战2.1 Smallest Difference
- 黑马程序员--多线程
- 5、构造器、类变量和单例
- c++运算符重载