设为首页 加入收藏

TOP

uva live 6190 Beautiful Spacing (二分+dp检验 根据特有性质优化)
2015-07-20 17:33:08 来源: 作者: 【 】 浏览:2
Tags:uva live 6190 Beautiful Spacing (二分 检验 根据 特有 性质 优化
I - Beautiful Spacing Time Limit:8000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit 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 words in the text must be placed keeping their original order. The following figures show correct and incorrect layout examples for a 4 word text "This is a pen" into 11 columns.

\ \

Figure I.1: A good layout.

Figure I.2: BAD " Do not reorder words.


Between two words adjacent in the same line, you must place at least one space character. You sometimes have to put more than one space in order to meet other conditions.

\

Figure I.3: BAD " Words must be separated by spaces.


A word must occupy the same number of consecutive columns as the number of characters in it. You cannot break a single word into two or more by breaking it into lines or by inserting spaces.

\

Figure I.4: BAD " Characters in a single word must be contiguous.


The text must be justified to the both sides. That is, the first word of a line must startfrom the first column of the line, and except the last line, the last word of a line must end at the last column.

\

Figure I.5: BAD " Lines must be 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 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.<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ICAgIDxlbT5XIE48YnI+CiAgICB4PC9lbT48c3ViPjE8L3N1Yj48ZW0+eDwvZW0+PHN1Yj4yPC9zdWI+IC4uLiA8ZW0+eDxzdWI+Tjwvc3ViPjwvZW0+PGJyPgo8L3A+CjxwPjxlbT5XPC9lbT4sIDxlbT5OPC9lbT4sIGFuZCA8ZW0+eDxzdWI+aTwvc3ViPjwvZW0+IGFyZSBhbGwgaW50ZWdlcnMuIDxlbT5XPC9lbT4gaXMgdGhlIG51bWJlciBvZiBjb2x1bW5zICgzIKHcIDxlbT5XPC9lbT4godwgODAsMDAwKS4gPGVtPk48L2VtPiBpcyB0aGUgbnVtYmVyIG9mIHdvcmRzICgyIKHcIDxlbT5OPC9lbT4godwgNTAsMDAwKS4gPGVtPng8c3ViPmk8L3N1Yj48L2VtPiBpcyB0aGUgbnVtYmVyIG9mIGNoYXJhY3RlcnMgaW4gdGhlIDxlbT5pPC9lbT4tdGgKIHdvcmQgKDEgodwgPGVtPng8c3ViPmk8L3N1Yj48L2VtPiCh3CAoPGVtPlc8L2VtPj8xKS8yICkuIE5vdGUgdGhhdCB0aGUgdXBwZXIgYm91bmQgb24gPGVtPng8c3ViPmk8L3N1Yj48L2VtPiBhc3N1cmVzIHRoYXQgdGhlcmUgYWx3YXlzIGV4aXN0cyBhIGxheW91dCBzYXRpc2Z5aW5nIHRoZSBjb25kaXRpb25zLjwvcD4KPHA+VGhlIGxhc3QgZGF0YXNldCBpcyBmb2xsb3dlZCBieSBhIGxpbmUgY29udGFpbmluZyB0d28gemVyb3MuPC9wPgo8aDI+T3V0cHV0PC9oMj4KPHA+Rm9yIGVhY2ggZGF0YXNldCwgcHJpbnQgdGhlIHNtYWxsZXN0IHBvc3NpYmxlIG51bWJlciBvZiB0aGUgbG9uZ2VzdCBjb250aWd1b3VzIHNwYWNlcyBiZXR3ZWVuIHdvcmRzLjwvcD4KPGgyPlNhbXBsZSBJbnB1dDwvaDI+CjxwcmUgY2xhc3M9"brush:java;">11 4 4 2 1 3 5 7 1 1 1 2 2 1 2 11 7 3 1 3 1 3 3 4 100 3 30 30 39 30 3 2 5 3 0 0

Output for the Sample Input

2
1
2
40
1

题意:给你一个含n个单词的文本,按照一些规则放入宽度为w的矩形中,怎样使最大的空格最小。 思路:答案是单调的,二分答案,然后用dp来检验,dp[i]表示第i个单词能否结尾,容易想到的是n^2检验,但是肯定会TLE的,需要优化。可以发现如果i-j能够放在一行,但是最大空格会超过mid,那么i+1-j也不行,如果用i来推的时候能够推到[s,t]可以,那么下次就可以直接从t+1开始了。 代码:
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
         #include 
         
           #include 
          
            #include 
           
             #include 
            
              #define maxn 50005 #define MAXN 200005 #define mod 1000000007 #define INF 0x3f3f3f3f #define eps 1e-6 const 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
             
              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; }
             
            
           
          
         
       
      
     
    
   
  
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++对文件的操作 下一篇POJ Hard Life (最大密度子图)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·JAVA现在的就业环境 (2025-12-26 01:19:24)
·最好的java反编译工 (2025-12-26 01:19:21)
·预测一下2025年Java (2025-12-26 01:19:19)
·Libevent C++ 高并发 (2025-12-26 00:49:30)
·C++ dll 设计接口时 (2025-12-26 00:49:28)