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 the words from the text one at a time, according to the order the words appear
in the text. The words are printed in lines, and we can print at most M characters in a line. If there is space available, we could print more than one word in a line. However, when we print more than one word in a line, we need to place exactly one
space character between two adjacent words in a line. For example, when we are given a text like the following:

This is a text of fourteen words and the longest word has ten characters

Now we can print this text into lines of no more than 20 characters as the following.

This is

a text of

fourteen words

and the longest

word

has ten characters

However, when you print less than 20 characters in a line, you need to pay a penalty, which is equal to the square of the difference between 20 and the actual number of characters you printed in that line. For example
in the first line we actually printed seven characters so the penalty is (20 − 7)2 = 169. The total penalty is the sum of all penalties from all lines. Given the text and the number of maximum number of characters allowed in a line, compute the
minimum penalty to print the text.

Input

The first line of the input is the number of test cases (C). The first line of a test case is the maximum number of characters allowed in a line (M). The second line of a test case is the number of
words in the text (N). The following N lines are the length (in character) of each word in the text. It is guaranteed that no word will have more than M characters, N is at most 10000, and M is at most 100.

Output

The output has C lines. Each line has the minimum penalty one needs to pay in order to print the text in that test case.

Sample Input

2
20
14
4
2
1
4
2
8
5
3
3
7
4
3
3
10
30
14
4
2
1
4
2
8
5
3
3
7
4
3
3
10

Sample Output

33
146

Source

题意  把n个单词排版   每行最多m个字符 不同单词间有空格 每行最后一个单词后没空格   空格占一个字符   当一行的字符数与m的差为t时 就会扣t*t分  求最少扣分

令a[i]表示第i个单词的长度   s[i]表示从第一个单词到第i个单词单词长度和d[i]表示前i个单词排版后最少扣的分

则t=m-(s[i] - s[j] + i - j - 1)表示把从第j+1个单词到第i个单词放在一行时这行的字符长度与m的差

那么当t>=0时  有转移方程 d[i]=min(d[i],d[j]+t*t) ;

有了方程程序就好写了:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10005;
int s[N], d[N], a[N], t, cas, m, n;
int main()
{
scanf ("%d", &cas);
while (cas--)
{
scanf ("%d%d", &m, &n);
for (int i = 1; i <= n; ++i)
{
scanf ("%d", &a[i]);
s[i] = s[i - 1] + a[i];
}
memset (d, 0x3f, sizeof (d)); d[0] = 0;
for (int i = 1; i <= n; ++i)
for (int j = i - 1; j >= 0; --j)
{
t = m - (s[i] - s[j] + i - j - 1);
if (t >= 0) d[i] = min (d[i], d[j] + t * t);
else break;
}
printf ("%d\n", d[n]);
}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ 3390 Print Words in Lines(DP)的更多相关文章

  1. poj 3390 Print Words in Lines 动态规划

    意甲冠军: 给n每行长度和字符可放置最大数量字m,每一行产生值至(m-x)^2,x是一个字符上线人数(包含空话之间格).为了让所有的完成,产生的话值最小和. 分析: 动态规划非常重要的就是状态的定义, ...

  2. POJ 2096 Collecting Bugs:期望dp

    题目链接:http://poj.org/problem?id=2096 题意: 有一个程序猿,他每天都会发现一个bug. bug共有n个种类.属于某一个种类的概率为1/n. 有s个子系统,每个bug属 ...

  3. poj - 1953 - World Cup Noise(dp)

    题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:id=1953" target="_blank">h ...

  4. POJ 1337 A Lazy Worker(区间DP, 背包变形)

    Description There is a worker who may lack the motivation to perform at his peak level of efficiency ...

  5. poj 1390 Blocks (经典区间dp 方块消除)

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4250   Accepted: 1704 Descriptio ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. poj 1417(并查集+简单dp)

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2087   Accepted: 640 Descrip ...

  8. 【POJ 2152】 Fire (树形DP)

    Fire   Description Country Z has N cities, which are numbered from 1 to N. Cities are connected by h ...

  9. poj 1141 Brackets Sequence(区间DP)

    题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...

随机推荐

  1. jquery经常使用事件(整理)

    Jquery事件 (一).事件列表. 1.blur() 当失去焦点时触发.包含鼠标点击离开和TAB键离开. 2.change() 当元素获取焦点后,值改变失去焦点事触发. 3.click() 当鼠标单 ...

  2. Teams(uva11609+组合)

    I - Teams Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit cid=7795 ...

  3. MEF初体验之六:导出和元素据

    在导出声明这一节中解释了部件导出服务和值的基础知识.在某些情况下,出于多种原因,关联与导出相关的信息是有必要的.通常,它被用来解释一个指定的普通契约实现的能力.这对于允许导入约束满足它的导出,或者导入 ...

  4. WPF学习(7)命令

    在上一篇中,我们学习了WPF的路由事件,而在本节将学习一个更为抽象且松耦合的事件版本,即命令.最明显的区别是,事件是与用户动作相关联的,而命令是那些与用户界面想分离的动作,例如我们最熟悉的剪切(Cut ...

  5. 走向DBA[MSSQL篇] 积跬步行千里

    原文:走向DBA[MSSQL篇] 积跬步行千里 不知道大家对SQL系列的感不感兴趣 先在这里探个路 本文针对的读者为SQL菜鸟 欢迎大牛驳论或者补充 既然是探路篇 就先说下数据过滤中的偏门匹配 希望能 ...

  6. sdut 在机器上面向对象编程练习11(运算符重载)

    在机器上面向对象编程练习11(运算符重载) Time Limit: 1000MS Memory limit: 65536K 标题叙述性说明 有两个矩阵a和b,均为2行3列,求两个矩阵之和.重载运算符& ...

  7. avalonjs1.5 入门教程

    迷你MVVM框架 avalonjs1.5 入门教程 avalon经过几年以后,已成为国内一个举足轻重的框架.它提供了多种不同的版本,满足不同人群的需要.比如avalon.js支持IE6等老旧浏览器,让 ...

  8. ORA-00838: Specified value of MEMORY_TARGET is too small(转)

    1. 测试环境OS: RHEL5U5(32bit)DB: Oracle 11.2.0.3.0(32bit) 2.   异常原因. 2.1 oracle 11g默认sga_target为0,如下图, O ...

  9. OpenGL于MFC使用汇总(三)——离屏渲染

    有时直接创建OpenGL形式不适合,或者干脆不同意然后创建一个表单,正如我现在这个项目,创建窗体不显示,它仅限于主框架.而我只是ActiveX里做一些相关工作,那仅仅能用到OpenGL的离屏渲染技术了 ...

  10. 朴素贝叶斯算法(Naive Bayes)

    朴素贝叶斯算法(Naive Bayes) 阅读目录 一.病人分类的例子 二.朴素贝叶斯分类器的公式 三.账号分类的例子 四.性别分类的例子 生活中很多场合需要用到分类,比如新闻分类.病人分类等等. 本 ...