4thweek.P_problemB .poj1505copy books.二分法

题目大意:

有m本书,k个抄写员。m本书的页码分别为 p1,p2……pm……。他们同时开始抄写且工作效率相同。要求分配给抄写员们若干书,每个抄写员要抄写的书的顺序必须是连续相邻的。

请你分配书使他们在最少的时间里完成抄写任务。

分析:

使每个抄写员分配到的页数的最大值尽可能小。最大值的最小化问题加二分法应用。

输入输出案例:

Sample Input

2
9 3
100 200 300 400 500 600 700 800 900
5 4
100 100 100 100 100

Sample Output

100 200 300 400 500 / 600 700 / 800 900
100 / 100 / 100 / 100 100

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5
 6 int n,k;
 7 int a[505];
 8 long long tot;
 9 int maxa;
10
11 void Init()
12 {
13 cin>>n>>k;
14 tot=0;
15 maxa=-1;
16 for(int i=0;i<n;i++)
17 {
18     cin>>a[i];
19     tot+=a[i];
20     maxa=max(a[i],maxa);  //明确最大页码的范围是:[maxa,tot].
21 }
22 }
23
24 int deal(long long num)
25 {
26     long long c=0;
27     int sum=1;
28     for(int i=0;i<n;i++)
29         if(c+a[i]<=num)
30            c+=a[i];
31         else
32         {
33             c=a[i];
34             sum++;
35         }
36     return sum;
37 }
38
39 void print(long long num)
40 {
41     int last[510];
42    long long done = 0;
43   memset(last, 0, sizeof(last));
44   int remain = k;
45   for(int i = n-1; i >= 0; i--)
46     {
47     if(done + a[i] > num || i+1 < remain)
48      {
49       last[i] = 1;
50       remain--;
51       done = a[i];
52      }
53     else
54       done += a[i];
55
56     }
57   for(int i = 0; i < n-1; i++)
58     {
59       printf("%d ", a[i]);
60       if (last[i])
61       printf("/ ");
62     }
63     printf("%d\n", a[n-1]);
64 }
65
66 void Do()
67 {
68   long long l,r,m;
69   l=maxa;
70   r=tot;
71   while(l<r)
72   {
73       m=l+(r-l)/2;   //二分,将抄写页码最大值区间缩小一半
74      if(deal(m)<=k) r=m;    //如果当前分配方法能分配额份数小于抄写员人数,则应把区间上限减小(区间缩小一半),
75      else l=m+1;         //如果当前分配使得分配份额大于抄写员人数,则把区间下限增大至m+1;
76   }
77   print(l);  // l>=r,此分配方案可行,输出
78 }
79
80 int main()
81 {
82     int T,t;
83     cin>>T;
84     t=T;
85     while(T--)
86     {
87         Init();
88         Do();
89     }
90     return 0;
91 }*/
时间: 2024-11-07 04:40:51

4thweek.P_problemB .poj1505copy books.二分法的相关文章

UVa714 Copying Books (二分法,贪心)

链接:http://vjudge.net/problem/UVA-714 分析:二分枚举最小值,用贪心的思想每段序列尽量往右划分,注意:因为要求字典序最小解,输出时还有一个贪心过程,左起S[i]尽量小,相当于右起S[j]尽量大,还有一种情况是剩下的数之和已经小于等于ans,但是此时剩余要分配的组数还有多(remain>1). 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 usin

4thweek.P_C poj3122 二分法

分配派问题题目大意: 我要过生日了,准备了n个不同半径大小的圆形的pie,我有f 个朋友要来参加我的聚会,我要将pie公平分配,且给每人(包括我自己)分配一块尽量大的pie.(因为碎块看起来不上台面.)求分配每个人的pie的体积的最大值.(pie的厚度一定,等于1) 分析: 运用二分法:将每个人能分配到的pie最大体积区间一直二分,直到不满足条件:while(left+0.0001<right).   体积区间为[left,right]. Input:One line with a positi

POJ1505 Copying Books(二分法)

B - 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so calledscribers. The scribe

uva 714 Copying Books (二分)

uva 714 Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its co

uva 714 - Copying Books(贪心 最大值最小化 二分)

题目描述开头一大堆屁话,我还仔细看了半天..其实就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,如果最小值相同情况下有多种分配方案,输出前面份数小的,就像字典序输出从小到大一样的意思. 这里用到贪心的方法,定义f(x)为真的条件是满足x为最大值使n本书分成k份,那么就是求x的最小值.如何确定这个x就是用的二分法,x一定大于0小于所有值的合,不断的二分再判断是否成立,成立就取左半边,不成立说明太小了就取右半边,写的时候还是没有把二分法理解透彻,我还怕会丢失

ACM最大值最小化&amp;&amp;二分法

Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so calledscribers. The scriber had been given a book and after several months he finished its copy. One of

uVa 714 (二分法)

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had

day05匿名函数,内置函数,二分法,递归,模块

yield作为表达式来使用的方式 #grep -rl 'python /root """ 查找root下文件中含有python的文件 """ import os def init(func): def wrapper(*args,**kwargs): g=func(*args,**kwargs) next(g) return g return wrapper @init def search(target): while True: search

714 - Copying Books——[贪心、二分查找]

Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most fa