首页 > 专题系列 > Java解POJ > POJ 1505 Copying Books [解题报告] Java
2013
11-09

POJ 1505 Copying Books [解题报告] Java

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 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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

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

解题代码:

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;

public class Main {
 static final int N = 500+10;
 static int n,m;
 static boolean flag[] = new boolean[N];
 static long sum[] = new long[N],num[] = new long[N];
 public static void main(String[]args) throws Exception{
  int t,i;
  long best;
  StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
  t = (int)Get_Num(cin);
  while(t--!=0){
	n = (int) Get_Num(cin);
	m = (int) Get_Num(cin);
	for(i=0;i< n;++i){
		num[i] = (long) Get_Num(cin);
		if(i==0) sum[i] = num[i];
		else sum[i] = num[i]+sum[i-1];
	}
	best = binary_search();
	make_ans(best);
  }
 }

 static void make_ans(long best){
  int i,j,k=0,last=n-1;
  Arrays.fill(flag, false);
  for(i=n-1;i>0;--i){
	if(sum[last]-sum[i-1]>best){
		flag[i] = true;
		last = i;
		++k;
	}
  }
  for(i=0;i< n && k< m-1;++i){
	if(flag[i]==false){
		flag[i] = true;
		++k;
	}
  }
		
  PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
  for(i=0;i< n;++i){
	if(i!=0) out.print(" ");
	out.print(num[i]);
	if(flag[i])
		out.print(" /");
  }
  out.println();
  out.flush();
		
  }
 static long binary_search(){
  long Min = num[0],Max = sum[n-1],Mid;
  while(Min+1< Max){
	Mid = (Min+Max)/2;
	if(can(Mid)) Max = Mid;
	else Min = Mid;
  }
  if(can(Min)) return Min;
   return Max;
 }

 static boolean can(long cnt){
	int i,must=0;
	long temp=0;
	for(i=0;i< n;++i){
		if(num[i]>cnt) return false;
		if(temp+num[i]>cnt){
			++must;
			temp = num[i];
		}
		else temp+=num[i];
	}
	if(temp>0) must++;
	if(must<=m) return true;
	return false;
   }
	static double Get_Num(StreamTokenizer cin)throws Exception{
		cin.nextToken();
		return cin.nval;
	}
}

  1. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。

  2. 为什么for循环找到的i一定是素数叻,而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak,而你每次取余都用的是原来的m,也就是n

  3. 你的理解应该是:即使主持人拿走一个箱子对结果没有影响。这样想,主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率,但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3

  4. /*
    * =====================================================================================
    *
    * Filename: 1366.cc
    *
    * Description:
    *
    * Version: 1.0
    * Created: 2014年01月06日 14时52分14秒
    * Revision: none
    * Compiler: gcc
    *
    * Author: Wenxian Ni (Hello World~), [email protected]
    * Organization: AMS/ICT
    *
    * =====================================================================================
    */

    #include
    #include

    using namespace std;

    int main()
    {
    stack st;
    int n,i,j;
    int test;
    int a[100001];
    int b[100001];
    while(cin>>n)
    {
    for(i=1;i>a[i];
    for(i=1;i>b[i];
    //st.clear();
    while(!st.empty())
    st.pop();
    i = 1;
    j = 1;

    while(in)
    break;
    }
    while(!st.empty()&&st.top()==b[j])
    {
    st.pop();
    j++;
    }
    }
    if(st.empty())
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    return 0;
    }

  5. /*
    * =====================================================================================
    *
    * Filename: 1366.cc
    *
    * Description:
    *
    * Version: 1.0
    * Created: 2014年01月06日 14时52分14秒
    * Revision: none
    * Compiler: gcc
    *
    * Author: Wenxian Ni (Hello World~), [email protected]
    * Organization: AMS/ICT
    *
    * =====================================================================================
    */

    #include
    #include

    using namespace std;

    int main()
    {
    stack st;
    int n,i,j;
    int test;
    int a[100001];
    int b[100001];
    while(cin>>n)
    {
    for(i=1;i>a[i];
    for(i=1;i>b[i];
    //st.clear();
    while(!st.empty())
    st.pop();
    i = 1;
    j = 1;

    while(in)
    break;
    }
    while(!st.empty()&&st.top()==b[j])
    {
    st.pop();
    j++;
    }
    }
    if(st.empty())
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    return 0;
    }