uva live 6190 Beautiful Spacing (二分+dp檢驗 根據特有性質最佳化),6190spacing

來源:互聯網
上載者:User

uva live 6190 Beautiful Spacing (二分+dp檢驗 根據特有性質最佳化),6190spacing
I - Beautiful SpacingTime Limit:8000MS     Memory Limit:65536KB     64bit IO Format:%lld & %lluSubmit 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 have to be satisfied.

The text is the most beautifully laid out when there is no unnecessarily long spaces. For instance, the layout in Figure I.6 has at most 2 contiguous spaces, which is more beautiful than that in Figure I.1, having 3 contiguous spaces. Given an input text and the number of columns, please find a layout such that 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
    x
1x2 ... xN

W, N, and xi are 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 on xi assures that there always exists a layout satisfying the conditions.

The last dataset is followed by a line containing two 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
題意:給你一個含n個單詞的文本,按照一些規則放入寬度為w的矩形中,怎樣使最大的空格最小。思路:答案是單調的,二分答案,然後用dp來檢驗,dp[i]表示第i個單詞能否結尾,容易想到的是n^2檢驗,但是肯定會TLE的,需要最佳化。可以發現如果i-j能夠放在一行,但是最大空格會超過mid,那麼i+1-j也不行,如果用i來推的時候能夠推到[s,t]可以,那麼下次就可以直接從t+1開始了。代碼:
#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;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.