uva live 6190 Beautiful Spacing (二分法+dp试 基于优化的独特性质)
Time Limit:8000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
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 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.
W N
x1x2 ... 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 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
#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-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<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;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
uva live 6190 Beautiful Spacing (二分法+dp试 基于优化的独特性质)的更多相关文章
- UVALive 6190 Beautiful Spacing (2012 Tokyo regional)
Beautiful Spacing 题意是给一个文本排版,求在满足题目所给要求的条件下,最长连续空格最小是多少. trick: 贪心地模拟是错的,至少无法证明正确性. 正解应该是二分答案+验证. 比较 ...
- 【BZOJ-4692】Beautiful Spacing 二分答案 + 乱搞(DP?)
4692: Beautiful Spacing Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 46 Solved: 21[Submit][Statu ...
- UVA 10163 Storage Keepers(两次DP)
UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- UVA - 825Walking on the Safe Side(dp)
id=19217">称号: UVA - 825Walking on the Safe Side(dp) 题目大意:给出一个n * m的矩阵.起点是1 * 1,终点是n * m.这个矩阵 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- dp的斜率优化
对于刷题量我觉得肯定是刷的越多越好(当然这是对时间有很多的人来说. 但是在我看来我的确适合刷题较多的那一类人,应为我对知识的应用能力并不强.这两天学习的内容是dp的斜率优化.当然我是不太会的. 这个博 ...
- HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化
HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
随机推荐
- 使用Python在2M内存中排序一百万个32位整数
译言网 | 使用Python在2M内存中排序一百万个32位整数 使用Python在2M内存中排序一百万个32位整数 译者:小鼠 发表时间:2008-11-13浏览量:6757评论数:2挑错数:0 作者 ...
- 【编程之美】java二进制实现重建
package com.cn.binarytree.utils; /** * @author 刘利娟 liulijuan132@gmail.com * @version 创建时间:2014年7月20日 ...
- 欧舒丹 L'Occitane 活力清泉保湿面霜 - 男士护肤 - 香港草莓网StrawberryNET.com
欧舒丹 L'Occitane 活力清泉保湿面霜 - 男士护肤 - 香港草莓网StrawberryNET.com 欧舒丹 活力清泉保湿面霜 50ml/1.7oz
- hdu4280(最大流)
传送门:Island Transport 题意:有N个岛屿 M条无向路 每个路有一最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到最东的那个岛屿. 分析:无向图正反都加弧,权值一样,这题点多, ...
- php 双向队列类
(deque,全名double-ended queue)是一种具有队列和栈的性质的数据结构.双向队列中的元素能够从两端弹出,其限定插入和删除操作在表的两端进行. 在实际使用中,还能够有输出受限的双向队 ...
- jQuery 弹出窗口的形式一直是具体案件的中心
在网上查 多 不是不符合无效;因此,一些自己总结,解决这个问题 原则: 常见问题: 弹出层居中了,背景也是半透明的 可是发现一拉动滚动栏立即就露馅了发现背景仅仅设置了屏幕所在段,其它部分都是原来 ...
- AC自动机---个人总结
比较好的 AC自动机算法详解.. [转]http://www.cppblog.com/mythit/archive/2009/04/21/80633.html 个人总结:[图是盗用的..] ac自动机 ...
- Android中Broadcast Receiver组件具体解释
BroadcastReceiver(广播接收器)是Android中的四大组件之中的一个. 以下是Android Doc中关于BroadcastReceiver的概述: ①广播接收器是一个专注于接收广播 ...
- Trie图和Fail树
Trie图和AC自动机的区别 Trie图是AC自动机的确定化形式,即把每个结点不存在字符的next指针都补全了.这样做的好处是使得构造fail指针时不需要next指针为空而需要不断回溯. 比如构造ne ...
- JPush极光推送 Java调用服务器端API开发
极光推送是:使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验.简单的说就是通过JPush后台管理网站进行app消息的推送.可以让用户及时 ...