Algorithm
Problem link- https://www.spoj.com/problems/BOOKS1/
BOOKS1 - 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.
Input
The input consists of N cases (equal to about 200). 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.
Example
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
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
#define MOD 1000000007LL
#define pb push_back
#define EPS 1e-9
#define FOR(i,n) for(int i = 0;i < n; i++)
#define FORE(i,a,b) for(int i = a;i <= b; i++)
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define F first
#define S second
#define sp ' '
#define M_PI 3.14159265358979323846
template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
const int MAXN = 505;
ll m, k;
ll arr[MAXN];
bool isPossible(int mid){
// maxpossible pages assigned can be mid
ll persons = 0;
int i = 0;
ll sum = 0;
while(i < m){
sum += arr[i];
if(sum > mid){
if(arr[i] > mid)
return 0;
sum = arr[i];
persons++;
}
i++;
}
if(sum != 0)
persons++;
// cout << mid << sp << persons << endl;
return (persons <= k);
}
int main(){
io;
int t;
cin >> t;
while(t--){
cin >> m >> k;
FOR(i, m) cin >> arr[i];
ll minn = INT_MAX;
FOR(i, m)
minn = min(minn, arr[i]);
ll maxx = 0;
FOR(i, m> maxx += arr[i];
ll lo = minn-1, hi = maxx;
// cout << lo << sp << hi << endl;
while(hi-lo > 1){
int mid = (lo+hi)/2;
if(isPossible(mid))
hi = mid;
else
lo = mid;
}
// cout << hi << endl;
int i = m-1;
ll sum = 0;
vector<int> V;
int numSlashes = 0;
while(i >= 0){
sum += arr[i];
if(sum > hi){
sum = arr[i];
if(i != m-1){
numSlashes++;
V.pb(-1);
}
}
V.pb(arr[i]);
i--;
}
// cout << numSlashes << sp << k-1 << endl;
for(int i = V.size()-1; i >= 0; i--){
if(V[i] == -1){
cout << "/ ";
}
else{
cout << V[i] << sp;
if((i-1) >= 0 && V[i-1] != -1 && numSlashes < (k-1)){
// cout << "YSE" << endl;
cout << "/ ";
numSlashes++;
}
}
}
cout << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
9 3
100 200 300 400 500 600 700 800 900
5 4
100 100 100 100 100
Output
900
100 / 100 / 100 / 100 100
#2 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.util.*;
import static java.lang.Integer.parseInt;
public class books1
{
private static BufferedReader f;
public static void main(String[] args) throws IOException {
f = new BufferedReader(new InputStreamReader(System.in));
int T = parseInt(f.readLine());
StringTokenizer st;
int[] books, splits; int num, s, i, j, lo, hi, mid;
while(T-- > 0)
{
st = new StringTokenizer(f.readLine());
num = parseInt(st.nextToken());
s = parseInt(st.nextToken());
st = new StringTokenizer(f.readLine());
books = new int[num];
lo = hi = 0;
for(i = 0; i < num; i++)
{
books[i] = parseInt(st.nextToken());
hi += books[i];
lo = Math.max(lo, books[i]);
}
splits = new int[s-1];
while(lo < hi)
{
mid = (lo+hi)>>1;
if(assign(mid, splits, books)) hi = mid;
else lo = mid+1;
}
assign(lo, splits, books);
System.out.print(books[0]);
j = 0;
for(i = 1; i < num; i++)
{
if(j < splits.length && i == splits[j])
{
System.out.print(" /");
j++;
}
System.out.print(" " + books[i]);
}
System.out.println();
System.out.flush();
}
System.exit(0);
}
private static boolean assign(int num, int[] splits, int[] books)
{
int s = splits.length;
int sum = 0;
for(int i = books.length-1; i >= 0; i--)
{
sum += books[i];
if(sum >= num)
{
if(sum > num) i++;
sum = 0;
if(s <= 0) return false;
splits[--s] = i;
}
if(i == s)
{// assign all books to remaining scribes
for(; i > 0; i--)
splits[--s] = i;
break;
}
}
return s == 0 && sum < num;
}
}
Copy The Code &
Try With Live Editor
Input
9 3
100 200 300 400 500 600 700 800 900
5 4
100 100 100 100 100
Output
900
100 / 100 / 100 / 100 100
Demonstration
SPOJ Solution-Copying Books-Solution in C, C++, Java, Python