Print words in lines
| Time limit:1000 ms |
|
Memory limit:65536 K |
| 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 mostMCharacters in a line. if there is space available, we cocould 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
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 followingNLines are the length (in character) of each word in the text. It is guaranteed that no word will have moreMCharacters,NIs at most 10000, andMIs at most 100.
Output
The output hasCLines. Each line has the minimum penalty one needs to pay in order to print the text in that test case.
Sample Input
220144214285337433103014421428533743310
Sample output
33146
Source
Kaohsiung 2006
Meaning meaning to typeset n words each line can contain a maximum of M characters. Different words have spaces. Each line has a last word without spaces. Space occupies one character. When the difference between the number of characters in a line and M is t T * t points are deducted for the minimum score.
Make a [I] the length of the I word s [I] indicates the length of the first word to the I word and D [I] indicates the layout of the first I word minimum points deducted
T = m-(s [I]-s [J] + I-j-1) indicates the length difference between the characters in the line from J + 1 to I
When T> = 0, there is a transfer equation d [I] = min (d [I], d [J] + T * t );
With the equation program, you can easily write:
#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]); }}