10259_PrintWordsinLines

2022-5-16 18:16| 发布者: Hocassian| 查看: 62| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-3-89504705826700-Problem List-采集的数据-后羿采集器.html

Pro.ID

10259

Title

Print Words in Lines

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=10259

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 1000/500 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    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.

    输入

    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.

    输出

    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

    样例输入

    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

    样例输出

    33
    146

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部