(UVA) Formula 1 - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2935
We need to associate the information about the positions in each race and the table of points. It will give the high score, which will allow us to check the pilots who have scored the maximum point.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
int numGrandPrix = sc.nextInt();
int numPilots = sc.nextInt();
while (numGrandPrix != 0 || numPilots != 0) {
int[][] result = new int[numGrandPrix][numPilots+1];
// read the results of the races
for (int i = 0; i < numGrandPrix; i++) {
for (int j = 1; j <= numPilots; j++) {
int position = sc.nextInt();
result[i][position] = j;
}
}
int numScoringSyst = sc.nextInt();
for (int i = 0; i < numScoringSyst; i++) {
int maxPilots = sc.nextInt();
int[] tablePoints = new int[maxPilots+1];
// read the table of points
for (int j = 1; j <= maxPilots; j++) {
tablePoints[j] = sc.nextInt();
}
int[] points = new int[numPilots+1];
// associate the positions with the table of points
for (int j = 1; j <= maxPilots; j++) {
for (int k = 0; k < numGrandPrix; k++) {
// result[k][j] gives who is in the jth position in the kth grand prix
points[result[k][j]] += tablePoints[j];
}
}
int maxPoint = 0;
// find the maximum point
for (int j = 1; j <= numPilots; j++) {
maxPoint = Math.max(maxPoint, points[j]);
}
ArrayList<Integer> worldChampion = new ArrayList<>();
// find all the pilots who have scored the maximum point
for (int j = 1; j <= numPilots; j++) {
if (points[j] == maxPoint) {
worldChampion.add(j);
}
}
// retrieve the answer
for (int j = 0; j < worldChampion.size()-1; j++) {
System.out.print(worldChampion.get(j) + " ");
}
System.out.println(worldChampion.get(worldChampion.size()-1));
}
numGrandPrix = sc.nextInt();
numPilots = sc.nextInt();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
We need to associate the information about the positions in each race and the table of points. It will give the high score, which will allow us to check the pilots who have scored the maximum point.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
int numGrandPrix = sc.nextInt();
int numPilots = sc.nextInt();
while (numGrandPrix != 0 || numPilots != 0) {
int[][] result = new int[numGrandPrix][numPilots+1];
// read the results of the races
for (int i = 0; i < numGrandPrix; i++) {
for (int j = 1; j <= numPilots; j++) {
int position = sc.nextInt();
result[i][position] = j;
}
}
int numScoringSyst = sc.nextInt();
for (int i = 0; i < numScoringSyst; i++) {
int maxPilots = sc.nextInt();
int[] tablePoints = new int[maxPilots+1];
// read the table of points
for (int j = 1; j <= maxPilots; j++) {
tablePoints[j] = sc.nextInt();
}
int[] points = new int[numPilots+1];
// associate the positions with the table of points
for (int j = 1; j <= maxPilots; j++) {
for (int k = 0; k < numGrandPrix; k++) {
// result[k][j] gives who is in the jth position in the kth grand prix
points[result[k][j]] += tablePoints[j];
}
}
int maxPoint = 0;
// find the maximum point
for (int j = 1; j <= numPilots; j++) {
maxPoint = Math.max(maxPoint, points[j]);
}
ArrayList<Integer> worldChampion = new ArrayList<>();
// find all the pilots who have scored the maximum point
for (int j = 1; j <= numPilots; j++) {
if (points[j] == maxPoint) {
worldChampion.add(j);
}
}
// retrieve the answer
for (int j = 0; j < worldChampion.size()-1; j++) {
System.out.print(worldChampion.get(j) + " ");
}
System.out.println(worldChampion.get(worldChampion.size()-1));
}
numGrandPrix = sc.nextInt();
numPilots = sc.nextInt();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment