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;
} 
时间: 2024-10-27 01:52:50

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 Description We have a paragraph of text to print. A text is a sequence of words and each word consists of characters. When we print a text, we print th

【POJ 3668】Game of Lines

Game of Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6555   Accepted: 2439 Description Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i ha

POJ 1952 BUY LOW, BUY LOWER 动态规划题解

Description The advice to "buy low" is half the formula to success in the bovine stock market.To be considered a great investor you must also follow this problems' advice: "Buy low; buy lower" Each time you buy a stock, you must purcha

POJ 题目2796 Feel Good(动态规划)

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11041   Accepted: 3020 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated

POJ 1269 (直线相交) Intersecting Lines

水题,以前总结的模板还是很好用的. 1 #include <cstdio> 2 #include <cmath> 3 using namespace std; 4 5 const double eps = 1e-8; 6 7 int dcmp(double x) 8 { 9 if(fabs(x) < eps) return 0; 10 return x < 0 ? -1 : 1; 11 } 12 13 struct Point 14 { 15 double x, y;

POJ 1390 Blocks(记忆化搜索+动态规划)

POJ 1390 Blocks 砌块 时限:5000 MS   内存限制:65536K 提交材料共计: 6204   接受: 2563 描述 你们中的一些人可能玩过一个叫做"积木"的游戏.一行有n个块,每个盒子都有一个颜色.这是一个例子:金,银,铜,金.相应的图片如下: 图1如果一些相邻的盒子都是相同的颜色,并且它左边的盒子(如果它存在)和它的右边的盒子(如果它存在)都是其他颜色的,我们称它为"盒子段".有四个盒子段.那就是:金,银,铜,金.片段中分别有1,4,3,

状态压缩动态规划 -- 旅行商问题

旅行商问题: N个点(N<16)的带权有向图D,求一条路径,使得这条路经过每个点恰好一次, 并且路径上边的权值和最小(或者最大),或者求一条具有这样性质的回路. 状态压缩: 将二进制表示十进制数N的点集,比如: 10 = 0000000000001010 代表第1和3个点已经路过 18 = 0000000000010010 代表第1和4个点已经路过 一个整数就是一个点集, dp_arr[binary][to_]代表经过点集 binary 中,当前终点为to_, 且路径最短的值,若该状态不存在就是

BZOJ 3831: [Poi2014]Little Bird【动态规划】

Description In the Byteotian Line Forest there are   trees in a row. On top of the first one, there is a little bird who would like to fly over to the top of the last tree. Being in fact very little, the bird might lack the strength to fly there with

动态规划_leetcode300(LIS)

#coding=utf-8 # 递归1### 选与不选 经典的组合问题 class Solution1(object): def lengthOfLIS(self, nums): """ :type nums: List[int] :rtype: int """ self.res = 0 if not nums : return 0 ans = [] self.getLIS(nums,0,ans) return self.res def getL