(SPOJ) Olimpíadas - Solution
Link to the problem: http://br.spoj.com/problems/OLIMPJ09/
This problem is very similar to the previous one with the same name. The only difference is how it is sorted.
import java.io.*;
import java.util.*;
class Main {
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numCountries = reader(br);
int numGames = reader(br);
Classification[] classifications = new Classification[numCountries+1];
classifications[0] = new Classification(0, -1);
for (int i = 1; i <= numCountries; i++) {
classifications[i] = new Classification(i, 0);
}
for (int i = 0; i < numGames; i++) {
int gold = reader(br);
int silver = reader(br);
int bronze = reader(br);
classifications[gold].numMedals++;
classifications[silver].numMedals++;
classifications[bronze].numMedals++;
}
Arrays.sort(classifications);
for (int i = 0; i < numCountries; i++) {
if (i == numCountries-1) {
bw.write(classifications[i].id + "\n");
break;
}
bw.write(classifications[i].id + " ");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Classification implements Comparable<Classification> {
int id;
int numMedals;
public Classification(int id, int nM) {
this.id = id;
numMedals = nM;
}
public int compareTo(Classification c) {
if (c.numMedals-this.numMedals == 0) {
return this.id-c.id;
}
return c.numMedals-this.numMedals;
}
}
This problem is very similar to the previous one with the same name. The only difference is how it is sorted.
import java.io.*;
import java.util.*;
class Main {
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numCountries = reader(br);
int numGames = reader(br);
Classification[] classifications = new Classification[numCountries+1];
classifications[0] = new Classification(0, -1);
for (int i = 1; i <= numCountries; i++) {
classifications[i] = new Classification(i, 0);
}
for (int i = 0; i < numGames; i++) {
int gold = reader(br);
int silver = reader(br);
int bronze = reader(br);
classifications[gold].numMedals++;
classifications[silver].numMedals++;
classifications[bronze].numMedals++;
}
Arrays.sort(classifications);
for (int i = 0; i < numCountries; i++) {
if (i == numCountries-1) {
bw.write(classifications[i].id + "\n");
break;
}
bw.write(classifications[i].id + " ");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Classification implements Comparable<Classification> {
int id;
int numMedals;
public Classification(int id, int nM) {
this.id = id;
numMedals = nM;
}
public int compareTo(Classification c) {
if (c.numMedals-this.numMedals == 0) {
return this.id-c.id;
}
return c.numMedals-this.numMedals;
}
}
Comments
Post a Comment