(UVA) Station Balance - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=351
We need to try all the possible combinations for putting a specimen in a chamber.
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
class Main {
public int numChambers;
public int numSpecimens;
public double averageMass;
public int[] specimensMass;
public ArrayList<ArrayList<Integer>> chambersContent;
public ArrayList<ArrayList<Integer>> chambersContentBetter;
public double imbalanceBetter;
public double calcImbalance() {
double imbalance = 0;
for (int i = 0; i < numChambers; i++) {
double sumMass = 0;
for (int j = 0; j < chambersContent.get(i).size(); j++) {
sumMass += chambersContent.get(i).get(j);
}
imbalance += (Math.abs(sumMass-averageMass));
}
return imbalance;
}
public void rec(int specimenID) {
if (specimenID == numSpecimens) {
double imbalance = calcImbalance();
if (imbalance < imbalanceBetter) {
imbalanceBetter = imbalance;
for (int i = 0; i < numChambers; i++) {
chambersContentBetter.set(i, (ArrayList<Integer>) chambersContent.get(i).clone());
}
}
return;
}
boolean putEmpty = false;
for (int j = 0; j < numChambers; j++) {
if (chambersContent.get(j).size() == 2) {
continue;
}
if (chambersContent.get(j).size() == 0) {
if (putEmpty) {
continue;
}
putEmpty = true;
}
chambersContent.get(j).add(specimensMass[specimenID]);
rec(specimenID+1);
chambersContent.get(j).remove(chambersContent.get(j).size()-1);
}
}
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numCase = 0;
String line = br.readLine();
while (line != null) {
String[] s = line.split("\\s");
numChambers = Integer.parseInt(s[0]);
numSpecimens = Integer.parseInt(s[1]);
double sumMass = 0;
specimensMass = new int[numSpecimens];
line = br.readLine();
s = line.split("\\s");
for (int i = 0; i < numSpecimens; i++) {
specimensMass[i] = Integer.parseInt(s[i]);
sumMass += specimensMass[i];
}
averageMass = sumMass/numChambers;
chambersContent = new ArrayList<>();
chambersContentBetter = new ArrayList<>();
for (int i = 0; i < numChambers; i++) {
chambersContent.add(new ArrayList<Integer>());
chambersContentBetter.add(new ArrayList<Integer>());
}
imbalanceBetter = 20000000.0;
rec(0);
bw.write("Set #" + ++numCase + "\n");
for (int i = 0; i < numChambers; i++) {
bw.write(" " + i + ":");
for (int j = 0; j < chambersContentBetter.get(i).size(); j++) {
bw.write(" " + chambersContentBetter.get(i).get(j));
}
bw.write("\n");
}
DecimalFormat df = new DecimalFormat("0.00000");
bw.write("IMBALANCE = " + df.format(imbalanceBetter) + "\n\n");
line = br.readLine();
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
We need to try all the possible combinations for putting a specimen in a chamber.
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
class Main {
public int numChambers;
public int numSpecimens;
public double averageMass;
public int[] specimensMass;
public ArrayList<ArrayList<Integer>> chambersContent;
public ArrayList<ArrayList<Integer>> chambersContentBetter;
public double imbalanceBetter;
public double calcImbalance() {
double imbalance = 0;
for (int i = 0; i < numChambers; i++) {
double sumMass = 0;
for (int j = 0; j < chambersContent.get(i).size(); j++) {
sumMass += chambersContent.get(i).get(j);
}
imbalance += (Math.abs(sumMass-averageMass));
}
return imbalance;
}
public void rec(int specimenID) {
if (specimenID == numSpecimens) {
double imbalance = calcImbalance();
if (imbalance < imbalanceBetter) {
imbalanceBetter = imbalance;
for (int i = 0; i < numChambers; i++) {
chambersContentBetter.set(i, (ArrayList<Integer>) chambersContent.get(i).clone());
}
}
return;
}
boolean putEmpty = false;
for (int j = 0; j < numChambers; j++) {
if (chambersContent.get(j).size() == 2) {
continue;
}
if (chambersContent.get(j).size() == 0) {
if (putEmpty) {
continue;
}
putEmpty = true;
}
chambersContent.get(j).add(specimensMass[specimenID]);
rec(specimenID+1);
chambersContent.get(j).remove(chambersContent.get(j).size()-1);
}
}
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numCase = 0;
String line = br.readLine();
while (line != null) {
String[] s = line.split("\\s");
numChambers = Integer.parseInt(s[0]);
numSpecimens = Integer.parseInt(s[1]);
double sumMass = 0;
specimensMass = new int[numSpecimens];
line = br.readLine();
s = line.split("\\s");
for (int i = 0; i < numSpecimens; i++) {
specimensMass[i] = Integer.parseInt(s[i]);
sumMass += specimensMass[i];
}
averageMass = sumMass/numChambers;
chambersContent = new ArrayList<>();
chambersContentBetter = new ArrayList<>();
for (int i = 0; i < numChambers; i++) {
chambersContent.add(new ArrayList<Integer>());
chambersContentBetter.add(new ArrayList<Integer>());
}
imbalanceBetter = 20000000.0;
rec(0);
bw.write("Set #" + ++numCase + "\n");
for (int i = 0; i < numChambers; i++) {
bw.write(" " + i + ":");
for (int j = 0; j < chambersContentBetter.get(i).size(); j++) {
bw.write(" " + chambersContentBetter.get(i).get(j));
}
bw.write("\n");
}
DecimalFormat df = new DecimalFormat("0.00000");
bw.write("IMBALANCE = " + df.format(imbalanceBetter) + "\n\n");
line = br.readLine();
}
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