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 ...
随机推荐
- 根据PID获取进程名&根据进程名获取PID
Liunx中 通过进程名查找进程PID可以通过 pidof [进程名] 来查找.反过来 ,相同通过PID查找进程名则没有相关命令.在linux根目录中,有一个/proc的VFS(虚拟文件系统),系统当 ...
- JMS是一种应用于异步消息传递的标准API
JMS是一种应用于异步消息传递的标准API,作为Java平台的一部分,JMS可以允许不同应用.不同模块之间实现可靠.异步数据通信.一些概念 JMS provider An implementat ...
- crx
https://www.crx4chrome.com/down/770/crx/ Downloading crx file for Linkclump The Linkclump crx file y ...
- node+mongodb+WP构建的移动社交应用源码 分享
源码地址: https://github.com/kangkaisen/dreaming dreaming 详情介绍:http://www.bcmeng.com/dreaming/
- Linux下图形界面调试工具kdbg安装及測试
1.Ubuntu系统下安装 Ubuntu系统安装比較方便,直接apt-get即可 apt-get install kdbg 2.centos 安装 首先,在这个地址下下载rpm包.然后使用rpm命令安 ...
- COCOS学习笔记--单点触控
这篇博客来总结下cocos单点触控的相关内容: 我们在Layer类的源代码中能够看到.Layer类提供了一下4个与屏幕单点触控相关的回调方法: onTouchBegan().onTouchMoved( ...
- php标准库中的优先队列SplPriorityQueue怎么使用?(继承)
php标准库中的优先队列SplPriorityQueue怎么使用?(继承) 一.总结 1.new对象,然后通过insert方法和extract方法来使用,top方法也很常用. 2.类的话首先想到继承, ...
- 微信小程序从零开始开发步骤(三)
上一章节,我们分享了如何创建一个新的页面和设置页面的标题,这一章我们来聊聊底部导航栏是如何实现的.即点击底部的导航,会实现不同对应页面之间的切换. 我们先来看个我们要实现的底部导航栏的效果图:(三个导 ...
- [NPM] Run npm scripts when files change with onchange
In this lesson we will look at how we can setup our npm scripts to execute when the file system has ...
- vi/vim基本使用命令
vi/vim基本使用命令 一.总结 一句话总结:1.记住三种模式:命令行模式.插入模式.底行模式:2.记住两个按键功能:i和esc 二.vi/vim基本使用命令 vi/vim 基本使用方法本文介绍了v ...