(UVA) The Grand Dinner - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1190
The solution below used a Greedy approach to solve this problem.
More details about this solution can be found in the code below through the comments.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTeams = sc.nextInt();
int numTables = sc.nextInt();
while (numTeams != 0 || numTables != 0) {
Team[] teams = new Team[numTeams];
Table[] tables = new Table[numTables];
for (int i = 0; i < numTeams; i++) {
teams[i] = new Team(i, sc.nextInt());
}
for (int i = 0; i < numTables; i++) {
tables[i] = new Table(i, sc.nextInt());
}
Arrays.sort(teams); // sort in descending order
Arrays.sort(tables); // sort in descending order
ArrayList<TreeSet<Integer>> distribution = new ArrayList<>();
for (int i = 0; i < numTeams; i++) {
distribution.add(new TreeSet<Integer>());
}
// for each team, try to place everyone in an available table
boolean everyone = true;
for (int i = 0; i < numTeams; i++) {
int sizeTeam = teams[i].numPeople; // if there are x people in a team, it is necessary at least x available tables
for (int j = 0; j < numTables && teams[i].numPeople > 0; j++) {
// there is not any available table
if (tables[j].capacity == 0) {
continue;
}
distribution.get(teams[i].idTeam).add(tables[j].idTable+1);
teams[i].numPeople--;
tables[j].capacity--;
}
if (teams[i].numPeople > 0) {
everyone = false;
break;
}
}
if (!everyone) {
bw.write("0\n");
} else {
bw.write("1\n");
for (int i = 0; i < numTeams; i++) {
for (Integer j : distribution.get(i)) {
bw.write(j+" ");
}
bw.write("\n");
}
}
numTeams = sc.nextInt();
numTables = sc.nextInt();
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Team implements Comparable<Team> {
int idTeam;
int numPeople;
public Team(int id, int num) {
idTeam = id;
numPeople = num;
}
public int compareTo(Team t) {
return t.numPeople-this.numPeople;
}
}
class Table implements Comparable<Table> {
int idTable;
int capacity;
public Table(int id, int c) {
idTable = id;
capacity = c;
}
public int compareTo(Table t) {
return t.capacity-this.capacity;
}
}
The solution below used a Greedy approach to solve this problem.
More details about this solution can be found in the code below through the comments.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTeams = sc.nextInt();
int numTables = sc.nextInt();
while (numTeams != 0 || numTables != 0) {
Team[] teams = new Team[numTeams];
Table[] tables = new Table[numTables];
for (int i = 0; i < numTeams; i++) {
teams[i] = new Team(i, sc.nextInt());
}
for (int i = 0; i < numTables; i++) {
tables[i] = new Table(i, sc.nextInt());
}
Arrays.sort(teams); // sort in descending order
Arrays.sort(tables); // sort in descending order
ArrayList<TreeSet<Integer>> distribution = new ArrayList<>();
for (int i = 0; i < numTeams; i++) {
distribution.add(new TreeSet<Integer>());
}
// for each team, try to place everyone in an available table
boolean everyone = true;
for (int i = 0; i < numTeams; i++) {
int sizeTeam = teams[i].numPeople; // if there are x people in a team, it is necessary at least x available tables
for (int j = 0; j < numTables && teams[i].numPeople > 0; j++) {
// there is not any available table
if (tables[j].capacity == 0) {
continue;
}
distribution.get(teams[i].idTeam).add(tables[j].idTable+1);
teams[i].numPeople--;
tables[j].capacity--;
}
if (teams[i].numPeople > 0) {
everyone = false;
break;
}
}
if (!everyone) {
bw.write("0\n");
} else {
bw.write("1\n");
for (int i = 0; i < numTeams; i++) {
for (Integer j : distribution.get(i)) {
bw.write(j+" ");
}
bw.write("\n");
}
}
numTeams = sc.nextInt();
numTables = sc.nextInt();
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Team implements Comparable<Team> {
int idTeam;
int numPeople;
public Team(int id, int num) {
idTeam = id;
numPeople = num;
}
public int compareTo(Team t) {
return t.numPeople-this.numPeople;
}
}
class Table implements Comparable<Table> {
int idTable;
int capacity;
public Table(int id, int c) {
idTable = id;
capacity = c;
}
public int compareTo(Table t) {
return t.capacity-this.capacity;
}
}
Comments
Post a Comment