意甲冠军:

给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 动态规划的更多相关文章

  1. POJ 3390 Print Words in Lines(DP)

    Print Words in Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1624 Accepted: 864 D ...

  2. [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行,最 ...

  3. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

  4. POJ 3181 Dollar Dayz(高精度 动态规划)

    题目链接:http://poj.org/problem?id=3181 题目大意:用1,2...K元的硬币,凑成N元的方案数. Sample Input 5 3 Sample Output 5 分析: ...

  5. [poj] 1269 [zoj] 1280 Interesting Lines || 求两直线交点

    POJ原题 ZOJ原题 多组数据.每次给出四个点,前两个点确定一条直线,后两个点确定一条直线,若平行则输出"NONE",重合输出"LINE",相交输出" ...

  6. POJ 1269 (直线相交) Intersecting Lines

    水题,以前总结的模板还是很好用的. #include <cstdio> #include <cmath> using namespace std; ; int dcmp(dou ...

  7. POJ 3186 Treats for the Cows (动态规划)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  8. BZOJ2287 【POJ Challenge】消失之物 动态规划 分治

    原文链接http://www.cnblogs.com/zhouzhendong/p/8684027.html 题目传送门 - BZOJ2287 题意 有$n$个物品,第$i$个物品的体积为$w_i$. ...

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

    Description The advice to "buy low" is half the formula to success in the bovine stock mar ...

随机推荐

  1. 小米笔记本(13.3 I7) ubuntu14.04下网卡驱动安装

    ubuntu 内核升级到4.6.4(更高版本可能造成系统无法启动) kernel debian包下载地址 http://kernel.ubuntu.com/~kernel-ppa/mainline/v ...

  2. [React] Use React.cloneElement to Modify and Add Additional Properties to React Children

    In this lesson we'll show how to use React.cloneElement to add additional properties to the children ...

  3. iOS开发项目实战——Swift实现ScrollView滚动栏功能

    手机作为一个小屏设备,须要显示的信息往往无法在一个屏幕上显示,此时就须要使用到滚动栏,当然除了像TableView这样能够自带滚动功能的. 假设一个界面上View较多,那就必须要使用到ScrollVi ...

  4. web中的重定向与转发

    Redirect:重定向(客户端重定向) 是HTTP协议规定的一种机制:当client向server发送一个请求,要求获取一个资源时,在server接收到这个请求后发现请求的这个资源实际存放在另一个位 ...

  5. JS生成一个种子随机数(伪随机数)

    原文链接:https://geniuspeng.github.io/2016/09/12/js-random/ 最近有一个需求,需要生成一个随机数,但是又不能完全随机,就是说需要一个种子seed,se ...

  6. 【48.47%】【POJ 2524】Ubiquitous Religions

    Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32364 Accepted: 15685 Description There a ...

  7. hibernate级联保存问题

    异常:org.hibernate.TransientObjectException: object references an unsaved transient instance 解决方法: XML ...

  8. [Typescript] Generics using TypeScript

    In this lesson we cover the key reason why programming languages need generics. We then show how use ...

  9. 6.Swift教程翻译系列——Swift集合类型

    英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 Swift提供数组和字典两种集合类型.用来存储很多值的情况.数组有序的存储一组同 ...

  10. 利用performSelectorInBackground和performSelectorOnMainThread实现多线程

    NSObject类的performSelectorOnMainThread和performSelectorInBackground能够实现简单的多线程编程技术 1.- (void)performSel ...