(UVA) Fill the Containers - Solution
Link to the problem: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2408
Based on the sum of the content of all vessels, the solution below used Binary Search to find out the minimum possible capacity of the container with maximum capacity.
import java.io.*;
import java.util.*;
class Main {
public int[] vessels;
public int numVessels;
public int numContainers;
public int best;
// try to fill the numContainers with maxContainer (max)
public boolean simulate(int maxContainer) {
int index = 0;
for (int i = 0; i < numContainers; i++) {
int amount = 0;
while (index < numVessels && amount+vessels[index] <= maxContainer) {
amount += vessels[index];
index++;
}
}
if (index == numVessels) { // if all the vessels were emptied
return true;
}
return false;
}
public void binSearch(int lo, int hi) {
if (lo > hi) {
return;
}
int m = (lo+hi)/2;
boolean isPossible = simulate(m);
if (isPossible) {
best = m;
binSearch(lo, m-1);
} else {
binSearch(m+1, hi);
}
}
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while (sc.hasNext()) {
numVessels = sc.nextInt();
numContainers = sc.nextInt();
vessels = new int[numVessels];
int sum = 0;
for (int i = 0; i < numVessels; i++) {
vessels[i] = sc.nextInt();
sum += vessels[i];
}
best = -1;
binSearch(1, sum);
bw.write(best+"\n");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Based on the sum of the content of all vessels, the solution below used Binary Search to find out the minimum possible capacity of the container with maximum capacity.
import java.io.*;
import java.util.*;
class Main {
public int[] vessels;
public int numVessels;
public int numContainers;
public int best;
// try to fill the numContainers with maxContainer (max)
public boolean simulate(int maxContainer) {
int index = 0;
for (int i = 0; i < numContainers; i++) {
int amount = 0;
while (index < numVessels && amount+vessels[index] <= maxContainer) {
amount += vessels[index];
index++;
}
}
if (index == numVessels) { // if all the vessels were emptied
return true;
}
return false;
}
public void binSearch(int lo, int hi) {
if (lo > hi) {
return;
}
int m = (lo+hi)/2;
boolean isPossible = simulate(m);
if (isPossible) {
best = m;
binSearch(lo, m-1);
} else {
binSearch(m+1, hi);
}
}
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while (sc.hasNext()) {
numVessels = sc.nextInt();
numContainers = sc.nextInt();
vessels = new int[numVessels];
int sum = 0;
for (int i = 0; i < numVessels; i++) {
vessels[i] = sc.nextInt();
sum += vessels[i];
}
best = -1;
binSearch(1, sum);
bw.write(best+"\n");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment