poj 3390 Print Words in Lines 动态规划
意甲冠军:
给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;
}
poj 3390 Print Words in Lines 动态规划的更多相关文章
- POJ 3390 Print Words in Lines(DP)
Print Words in Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1624 Accepted: 864 D ...
- [CareerCup] 13.1 Print Last K Lines 打印最后K行
13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- POJ 3181 Dollar Dayz(高精度 动态规划)
题目链接:http://poj.org/problem?id=3181 题目大意:用1,2...K元的硬币,凑成N元的方案数. Sample Input 5 3 Sample Output 5 分析: ...
- [poj] 1269 [zoj] 1280 Interesting Lines || 求两直线交点
POJ原题 ZOJ原题 多组数据.每次给出四个点,前两个点确定一条直线,后两个点确定一条直线,若平行则输出"NONE",重合输出"LINE",相交输出" ...
- POJ 1269 (直线相交) Intersecting Lines
水题,以前总结的模板还是很好用的. #include <cstdio> #include <cmath> using namespace std; ; int dcmp(dou ...
- POJ 3186 Treats for the Cows (动态规划)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- BZOJ2287 【POJ Challenge】消失之物 动态规划 分治
原文链接http://www.cnblogs.com/zhouzhendong/p/8684027.html 题目传送门 - BZOJ2287 题意 有$n$个物品,第$i$个物品的体积为$w_i$. ...
- POJ 1952 BUY LOW, BUY LOWER 动态规划题解
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
随机推荐
- HDU 1284 钱币兑换问题 母函数、DP
题目链接:HDU 1284 钱币兑换问题 钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 【solr基础教程之二】索引 分类: H4_SOLR/LUCENCE 2014-07-18 21:06 3331人阅读 评论(0) 收藏
一.向Solr提交索引的方式 1.使用post.jar进行索引 (1)创建文档xml文件 <add> <doc> <field name="id"&g ...
- LinearLayout的一些注意事项 分类: H1_ANDROID 2013-10-26 23:01 856人阅读 评论(0) 收藏
1.orientation的默认值为horizontal,即从左向右排列.由于一般从上向下排列,所以必须指定orientation属性. 2.layout_gravity与gravity的区别: (1 ...
- [Compose] 13. Lift into a Pointed Functor with of
We examine the of function we've seen on a few types and discover it's the Pointed interface. Instea ...
- css选择器指定元素中第几个子元素
tr td:nth-child(2){ background-color:gray; } 就是tr当中的td的第二个td的属性 tr:nth-child(2n+0){ background-color ...
- Using Eredis, Redis With Erlang
http://no-fucking-idea.com/blog/2012/03/23/using-eredis-in-erlang/ Using Eredis, Redis With Erlang M ...
- iOS QLPreviewController(Quick Look)快速浏览jpg,PDF,world等
#import <QuickLook/QuickLook.h> @interface ViewController ()<QLPreviewControllerDataSource, ...
- BZOJ 2064 - 状压DP
传送门 题目大意: 给两个数组, 数组中的两个元素可以合并成两元素之和,每个元素都可以分裂成相应的大小,问从数组1变化到数组2至少需要多少步? 题目分析: 看到数据范围\(n<=10\), 显然 ...
- Notes on OpenSSL and Qt(ssl.pri,qsslocket_openssl_symbols.cpp)
Libraries name of openssl? The "library" portion of OpenSSL consists of two libraries. On ...
- POJ 2728 - 最小比例生成树
传送门 题目大意 有n个村庄,每个村庄都有一个(x, y)坐标和z海拔,定义两个村庄间的dist为坐标的距离,cost为海拔差的绝对值,求图的一颗生成树,使得\(\frac{\sum cost}{\s ...