poj3390

Print Words in Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1592 Accepted: 842

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

题目大意就是填充单词呀,填充单词....

这道题还是看别人的思路写下来的...感觉自己弱爆了...

具体的过程是填写第i个单词时查找前面的i-1个单词继续填充比较与当前的较小值

当然前提是前面的单词加入后不能超过maxiumInput。。。

下面贴代码...

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define MAXLEN 101
#define MAX 10001
using namespace std;
int main()
{
    int Times;
    scanf("%d",&Times);
    int wordLen[MAX];
    int dp[MAX];
    while(Times--)
    {
        int maxiumInput,num;
        scanf("%d",&maxiumInput);
        scanf("%d",&num);
        for(int i=1;i<=num;i++)
        {
                scanf("%d",&wordLen[i]);
        }
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=num;i++)
        {
                dp[i] = dp[i-1]+pow((double)(maxiumInput-wordLen[i]),2);
                int Sum =wordLen[i];
                for(int j=i-1;j>0&&maxiumInput-Sum>0;j--)
                {
                        Sum+=(wordLen[j]+1);
                        if(maxiumInput-Sum>=0)
                            dp[i] =min(dp[i],dp[j-1]+(int)pow((double)(maxiumInput-Sum),2));//第一次竟然写成了dp[j]....呵呵
                }
        }
        printf("%d\n",dp[num]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值