I-Beautiful SpacingTime
limit:8000MS
Memory Limit:65536KB
64bit IO Format:%lld & Amp %llu Submit
cid=57803#status//i/0 "class=" Ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only "style=" Font-family:verdana,arial,sans-serif; Font-size:1em; border:1px solid RGB (211,211,211); Background-color:rgb (227,228,248); Color:rgb (85,85,85); Display:inline-block; position:relative; padding:0px; Margin-right:0.1em; Zoom:1; overflow:visible; Text-decoration:none ">Status
Description
Text is a sequence of words, and a word consists of characters. Your task is to put words into a grid with W columns and sufficiently many lines. For the beauty of the layout, the following conditions has to be satisfied.
- The words in the text must is placed keeping their original order. The following figures show correct and incorrect layout examples for a 4 word text ' This is a pen ' into one columns.
|
|
Figure i.1:a Good layout. |
Figure I.2:bad | Do not reorder words. |
- Between-words adjacent in the same line, you must place at least one space character. You sometimes has to put more than one space in order to meet and other conditions.
Figure I.3:bad | Words must is separated by spaces.
- A word must occupy the same number of consecutive columns as the number of characters in it. You cannot breaks a single word into the or more by breaking it into the lines or by inserting spaces.
Figure I.4:bad | Characters in a single word must is contiguous.
- The text must is justified to the both sides. That's, the first word of a line must startfrom the first column of the line, and except, the last word of A line must end on the last column.
Figure I.5:bad | Lines must is Justi ed to both, the left and the right sides.
The text is the most beautifully laid out when there is no unnecessarily long spaces. For instance, the "layout in" figure i.6 have at most 2 contiguous spaces, which are more beautiful than this in Figure I.1, H Aving 3 contiguous spaces. Given an input text and the number of columns, please find a layout such the length of the longest contiguous spaces Between words is minimum.
Figure I.6:a Good and the most beautiful layout.
Input
The input consists of multiple datasets, each in the following format.
W N
X1x2 ... XN
W, N, and XI is all integers. W is the number of columns (3≤ W ≤80,000). n is the number of words (2≤ N ≤50,000). XI is the number of characters in the I-th word (1≤ xi ≤ (W? 1)/2). Note that the upper bound in XI assures that there always exists a layout satisfying the conditions.
The last dataset was followed by a line containing the zeros.
Output
For each dataset, print the smallest possible number of the longest contiguous spaces between words.
Sample Input
11 44 2 1 35 71 1 1 2 2 1 211 73 1 3 1 3 3 4100 330 30 3930 32 5 30 0
Output for the Sample Input
212401
Test Instructions:give you a text with n words. In a rectangle with a width of w in accordance with some rules, how to make the largest space minimum. Ideas:The answer is monotonous, two-point answer, and then use DP to test, Dp[i] Indicates whether the first word can end, easy think of is n^2 test, but certainly will tle, need to optimize. Can be found to assume that i-j can be placed in a row, but the maximum space will exceed mid, then i+1-j also not, assuming I to push the time can be pushed to [s,t] can, then next time can be directly from the t+1 start.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #define MAXN 50005#define maxn 200005#define mod 1000000007#define INF 0x3f3f3f3f#define EPS 1e -6const double Pi=acos ( -1.0); typedef long Long ll;using namespace Std;int n,w;int len[maxn],sum[maxn];bool dp[maxn];bool isOK (int mid) {int i,j,last=0; Memset (Dp,0,sizeof (DP)); Dp[0]=1; if (sum[n]+n-1<=w) return true; for (i=0; i<n-1; i++) {if (!dp[i]) continue; For (J=max (i+2,last+1); j<=n; J + +) {if (w<sum[j]-sum[i]+j-i-1) break; if (W>sum[j]-sum[i]+ll (j-i-1) *mid) continue; Last=j; Dp[j]=1; if (sum[n]-sum[j]+n-j-1<=w) return true; }} return false;} void Solve () {int i,j,le=1,ri=w,mid,ans; while (Le<=ri) { Mid= (Le+ri) >>1; if (isOK (mid)) {Ans=mid; Ri=mid-1; } else le=mid+1; } printf ("%d\n", ans);} int main () {int i,j; while (~SCANF ("%d%d", &w,&n)) {if (w==0&&n==0) break; sum[0]=0; for (I=1; i<=n; i++) {scanf ("%d", &len[i]); Sum[i]=sum[i-1]+len[i]; } solve (); } return 0;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
UVA Live 6190 Beautiful Spacing (dichotomy +DP test based on the unique nature of optimization)