您的位置:首页 > 运维架构

8.6-244-uva714-Copying books-二分法-最大值尽量小

2018-01-21 19:10 477 查看
题目:

把一个包含m个正整数的序列划分(划分表面不相交)成k个非空连续子序列

设第i个序列的和为S(i) 找一个划分使最大的S(i)(记为maxp)的尽量小

多种方案优先取S(1)最小,,S(1)相同S(2)最小,,,依此类推。

思路:

记P(x)为将序列划分为m个,maxp=x,满足m<=k;(具体实现用solve(x)<=k)这样就有了二分法的判断条件(用二分法快速找到最小的x)

最终找到的x即为maxp

k表示对应需要‘/’的最小个数

对于5个元素的数组 1 1 1 1 1

maxp与k关系如下

maxp12345
k43或2110
题目的K取4时
函数solve()采用贪心法 

取maxp=2时,算出的k是唯一的等于3(而4事实上也满足 但贪心总是选择最小的)

此时solve(maxp)<=K 这时会尝试pmax=1但返回的是k=5>K 最终pmax还是等于2

(事实上由lower_bound()的性质 插入的位置①能保持有序②位于最左 当k=4时 插入的位置1,2,3,4,5对应的函数值5,3,2,2,1没有4,这时必会插入到2 的位置 即maxp=2)

因此以maxp=2 直接用贪心法倒着(必须倒着)打印会出错 最终只会打印出3个‘/’

即1 / 1 1/ 1 1  正确答案应为1/ 1/1 / 1 1 

因此需要补充‘/’由题目前面S(i)尽量小的限制条件 ‘/’需要从最左依次往右补

不难发现前面的数字每个后面都跟着一个‘/’

  得到判别方法i+1<remain

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=500+5;
int m,k,p[maxn];
int solve(long long maxp){
long long done=0;
int ans=1;
for(int i=m-1;i>=0;i--){   //正序倒序皆可
if(done+p[i]<=maxp) done+=p[i];   //贪心法 得到尽量小的ans(‘/’的个数)
else {
done=p[i];
ans++;
}
}
return ans;
}
int last[maxn];
void print(long long ans) {
long long done=0;
memset(last,0,sizeof(last));
int remain=k;
for(int i=m-1;i>=0;i--){
if(done+p[i]>ans||i+1<remain) {  //剩余数字个数小于剩余/数量时(事实上只会差一个)必须放一个/  i+1<remain第一次成立后后面会一直成立
done=p[i];
last[i]=1;
remain--;
}
else {
done+=p[i];
}

}
for(int i=0;i<m-1;i++){
cout<<p[i]<<" ";
if(last[i]) cout<<"/ ";
}
cout<<p[m-1]<<endl; //最后一个单独处理 没有空格

}
int main(){
int T;
cin>>T;
while(T--){

cin>>m>>k;
long long tot=0;
int maxp=-1;
for(int i=0;i<m;i++){
cin>>p[i];
tot+=p[i];    //数组之和
maxp=max(maxp,p[i]);//元素最大值
}
long long L=maxp,R=tot;
while(L<R){                 //二分查找
long long M=L+(R-L)/2;
if(solve(M)<=k) R=M;
else L=M+1;
}
print(L);
}
system("pause");
return 0;
}


714     Copying Books
Before the invention ofbook-printing, it was very hard to make a copy of a book. All the contents hadto be re-written by hand by so calledscribers.The scriber had been given a book and after several months he finished itscopy.
One of the most famous scribers lived in the 15th century and his namewas Xaverius Endricus Remius
Ontius Xendrianus (Xerox). Anyway, the work was veryannoying and boring. And the only way to speed it up was to hire more scribers.

Once upon a time,there was a theater ensemble that wanted to play famous Antique Tragedies. Thescripts of these plays were divided into many books and actors needed morecopies of them, of course.
So they hired many scribers to make copies of thesebooks. Imagine you have
mbooks (numbered

1,2,...,m) that may have different number of pages (p1,p2,...,pm)
and you want to makeone copy of each of them. Your task is to divide these books amongk
scribes, k≤m. Each bookcan be assigned to a single scriber only, and every scriber must get acontinuous sequence of books. That means, there
exists an increasing successionof numbers0= b0
< b1< b2,... < bk−1 ≤bk
= m such that
i-th scribergets a sequence of books with numbers betweenbi−1+1and
bi. The time needed to make a copy of all the books is determined bythe scriber who was assigned the most work. Therefore, our goal is to minimizethe
maximum number of pages assigned to a single scriber. Your task is to findthe optimal assignment.

Input

The input consists ofN
cases. The first line of the input contains only positive integerN. Then follow the cases. Each case consists of exactly two lines. Atthe first line,
there are two integersm and
k, 1≤ k
≤ m≤500. At the second line, there are integersp1,p2,...,pmseparated
by spaces.All these values are positive and less than 10000000.

Output

For each case, print exactly oneline. The line must contain the input successionp1,p2,...pmdivided
into exactly k parts such that themaximum sum of a single part should be as small as possible. Use the slashcharacter (‘/’) to separatethe parts.
There must be exactly one space character between any two successivenumbers and between the number and the slash.

If there ismore than one solution, print the one that minimizes the work assigned to thefirst scriber, then to the second scriber etc. But each scriber must beassigned at least one book.

Sample Input

2
93
100200 300 400 500 600 700 800 900
54
100100 100 100 100

Sample Output

100200 300 400 500 / 600 700 / 800 900
100/ 100 / 100 / 100 100
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二分法 贪心 束搜索