您的位置:首页 > 其它

uva oj 714解题报告

2010-04-16 13:31 330 查看

二分查找+贪心 ||degree - 3

1 . 题目描述

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

) that may have different number of pages (

) and you want to make one copy of each of them. Your task is to divide these books among k scribes,

. 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

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,

. At the second line, there are integers

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

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.

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


2 . 测试用例

input:
3
12 5
48 65 53 3 90 3 90 30 52 75 54 22
10 4
82 36 6 11 33 33 25 79 9 7
9 5
64 16 75 91 1 71 43 81 13

output:
48 65 / 53 3 90 / 3 90 30 / 52 75 / 54 22
82 / 36 6 11 / 33 33 25 / 79 9 7
64 / 16 75 / 91 1 / 71 43 / 81 13

3 . 算法描述

1. 主体思路:

题目要求算出单个“抄写者”能够抄写的最大值(在这里记为max),问题1怎么找出这个max?
问题1解答:具体实现为先计算出所有pages的合sum,然后在0-sum之间进行二分查找,问题2就是怎样判断当前的二分查找的值(mid)就是目标值(max)呢,或者当前的值是大于还是小于目标值?
问题2解答:这里用到了贪心算法,对于每一个scriber,按顺序(从后往前,因为题目要求如果有相似解的情况,后面scriber抄写的页数应该大于前面的)依次累加page,直到超过max的值。循环计算直到第一个scriber,计算余下pages的和,如果这个合的值大于当前的max,那么证明当前max的值偏小,二分查找应当向后搜索了;如果这个合的值小于当前max,则证明当前max的偏大的,二分查找向前搜索;如果找到则返回查找成功

2. 算法实现细节

从上面的描述可以看到程序中最关键的实现在于怎样确定二分查找的mid值与我们想要找到的max值之间的关系.
public static int judge(long mid){
boolean found = false;
long sum = 0;
int index = bookNum-1;
for (int i=scriberNum-1;i>0;i--){
sum = 0;
for (;index>=i&&(sum = sum + pages[index])<=mid;index--){
if (sum==mid){
found = true;
}
}

seperators[i-1] = index;
}
sum = 0;
for(;index>=0;index--){
sum = sum + pages[index];
if(sum > mid){
return BIG;
}
}
if(sum==mid){
return FOUND;
}else{
if (found){
return FOUND;
}else{
return SMALL;
}
}
}


4 . 程序中遇到的问题

实现这个程序中遇到的最大问题就是:计算部分合的过程中,出现有的抄写者没有分到书的情况(eg : input :5 4,100 100 100 100 100),这种情况在计算部分和的时候就有可能出现第四个与第三个抄写者分配到了200页但是第一个抄写者没有分配到书的情况。。
解决办法:
在对“抄写者”计算“页数部分合”的时候,除了要满足“部分合”少于“当前最大值”,还要满足余下的书必须能够保证足够分配给剩下的“抄写者”。主要是下面代码的判断部分......
for (int i=scriberNum-1;i>0;i--){
sum = 0;
for (;index>=i&&(sum = sum + pages[index])<=mid;index--){	//这里的"i"数值上等于剩下的抄写者人数
if (sum==mid){
found = true;
}
}

seperators[i-1] = index;
}


5 . 需要改进的地方



6 . 代码

package CopyingBooks_714;
import java.util.Scanner;
public class Main {
static final int SMALL = -1;
static final int BIG = 1;
static final int FOUND = 0;

static int bookNum;
static int scriberNum;
static int[] pages;
static int[] seperators;
static long sum;

public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int cases = in.nextInt();
for (int i=0;i<cases;i++){
bookNum = in.nextInt();
scriberNum = in.nextInt();
pages = new int[bookNum];
seperators = new int[scriberNum-1];
sum = 0;
for (int j =0;j<bookNum;j++){
pages[j] = in.nextInt();
sum = sum + pages[j];
}
work(0,sum);
}
}

public static void work(long begin,long end){
if (begin<=end){
long mid = begin + (end -begin)/2;
int result = judge(mid);
if (result == SMALL){
work(begin,mid-1);
}else if(result == BIG){
work(mid+1,end);
}else{
print(mid);
}
}
}

public static int judge(long mid){ boolean found = false; long sum = 0; int index = bookNum-1; for (int i=scriberNum-1;i>0;i--){ sum = 0; for (;index>=i&&(sum = sum + pages[index])<=mid;index--){ if (sum==mid){ found = true; } } seperators[i-1] = index; } sum = 0; for(;index>=0;index--){ sum = sum + pages[index]; if(sum > mid){ return BIG; } } if(sum==mid){ return FOUND; }else{ if (found){ return FOUND; }else{ return SMALL; } } }

public static void print(long max){
int s=0;
boolean stop =false;
int [] temp = seperators;
for(int i=0;i<bookNum-1;i++){
if(!stop&&i==seperators[s]){
System.out.print(pages[i]+" / ");
if(s<scriberNum-2){
s++;
}else{
stop = true;
}
}
else{
System.out.print(pages[i]+" ");
}
}
System.out.println(pages[bookNum-1]);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: