首页 > 代码库 > POJ 1505 (Copying Books)(经典二分)
POJ 1505 (Copying Books)(经典二分)
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 been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying 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. The scripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have m books (numbered 1, 2 ... m) that may have different number of pages (p1, p2 ... pm) and you want to make one copy of each of them. Your task is to divide these books among k scribes, k <= m. Each book can be assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers 0 = b0 < b1 < b2, ... < bk-1 <= bk = m such that i-th scriber gets a sequence of books with numbers between bi-1+1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.
Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have m books (numbered 1, 2 ... m) that may have different number of pages (p1, p2 ... pm) and you want to make one copy of each of them. Your task is to divide these books among k scribes, k <= m. Each book can be assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers 0 = b0 < b1 < b2, ... < bk-1 <= bk = m such that i-th scriber gets a sequence of books with numbers between bi-1+1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the first line, there are two integers m and k, 1 <= k <= m <= 500. At the second line, there are integers p1, p2, ... pm separated by spaces. All these values are positive and less than 10000000.
Output
For each case, print exactly one line. The line must contain the input succession p1, p2, ... pm divided into exactly k parts such that the maximum sum of a single part should be as small as possible. Use the slash character (‘/‘) to separate the parts. There must be exactly one space character between any two successive numbers and between the number and the slash.
If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.
If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.
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
Source
Central Europe 1998
题意:给你n本书,k个人翻译,问怎样分配一个人单独翻译的最大值最小,换而言之,每个人翻译的差不多,
还有一个要求,就是如果可以,尽量让第一个人翻译的少
思路:先二分找出一个人翻译的最大值,然后找出每个人的翻译的(从后到前找),有一个bug,让我cuo了11发,%>_<%
我在代码中说明
#include<iostream> #include<cstdio> #include<cstring> #include<cstdio> using namespace std; #define N 505 int a[N],vis[N]; int n,k; int seach(int mid) //每个人翻译mid本书,看看需要的人数能不能少于等于k { int step=1,cur=0,i; for(i=1;i<=n;i++) if(cur+a[i]<=mid) cur+=a[i]; else { step++; cur=a[i]; } if(step<=k) return 1; return 0; } int main() { int i,t,le,ri; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); le=ri=0; for(i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]>le) le=a[i]; ri+=a[i]; } int ans,mid; while(le<=ri) { mid=(le+ri)/2; if(seach(mid)) { ans=mid; ri=mid-1; } else le=mid+1; } memset(vis,0,sizeof(vis)); int x=k,j,temp=0; for(i=n;i>=1;i--) { if(x>i) //这是bug关键,如果人数比数的本书多,后面的人一人一本 { //这里情况很复杂 请认真结合我下面给的样例想想,=和>的区别 //我比赛时错了 11发(就是这没考虑) for(i;i>=1;i--) vis[i]=1; break; } temp+=a[i]; if(temp>ans) { temp=a[i]; vis[i]=1; x--; //人数减一 } } for(i=1;i<=n;i++) { if(i==1) printf("%d",a[i]); else printf(" %d",a[i]); if(vis[i]) printf(" /"); } printf("\n"); } return 0; } // n=4 k=4 //当ans=200 // 20 100 100 200 //如果我上面强调的(x>i),那么100 和 100之间不会有 / //还有为什么是 > 不是 >= 呢,再给一组数据 // n=6,k=5,ans=200 // 50 100 50 50 50 200 //结果 50/100/50/50/50/200,就会出错,请认真思考(毕竟我当时快崩溃了)
我错了11次,当时ac简直无法形容我的感觉,如有解释不当的地方,欢迎指出
上面二分简单就没有解释多少,请注意我强调的地方
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。