I'm trying to solve 10194-aka Soccer with a Java program, I tested various cases but always received WA answer from judge, I read another post witch says that nobody solve aka Soccer using Java. It's that right? (so they move to C++ for the solution).
Another people say: use case insensitive comparison for lexicographic in Sort, I tried toUpperCase, toLowerCase, compareToIgnoreCase but still WA answer.
Searching for a test case I found the following:
INPUT
1
Tour name
2
Brazil
Norway
1
brAzil#2@1#NoRwAy
My program give the following result:
Tour name
1) Brazil 3p, 1g (1-0-0), 1gd (2-1)
2) Norway 0p, 1g (0-0-1), -1gd (1-2)
But using UVA toolkit Problem Database and Solver (http://www.uvatoolkit.com/problemssolve.php) have the following result:
Tour name
1) Norway 0p, 0g (0-0-0), 0gd (0-0)
2) Brazil 0p, 1g (0-0-1), -1gd (1-2)
I think that my program gives the correct result (because using case insensitive comparison Brazil its equal to brAzil in games played for a team...).
Thinking something unimaginable. I have the following question: that the judge have the correct results for this problem? or am I wrong?
If you want to look my program, I attached it at the end of this post.
Regards
Marcelo Salas
Code: Select all
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Arrays;
import java.io.BufferedInputStream;
/**
*
* @author Lic. Marcelo Salas Vargas
* Problema: 10194 aka Soccer
*/
class Equipo implements Comparable<Equipo> {
String nombre;
int puntos_totales;
int partidos_jugados;
int partidos_ganados;
int partidos_empatados;
int partidos_perdidos;
int gol_diferencia;
int gol_a_favor;
int gol_en_contra;
public Equipo(String nombre) {
this.nombre=nombre;
this.puntos_totales=0;
this.partidos_jugados=0;
this.partidos_ganados=0;
this.partidos_empatados=0;
this.partidos_perdidos=0;
this.gol_diferencia=0;
this.gol_a_favor=0;
this.gol_en_contra=0;
}
public void SetScore(int gol_equipo,int gol_contrario) {
this.partidos_jugados++;
this.gol_a_favor=this.gol_a_favor+gol_equipo;
this.gol_en_contra=this.gol_en_contra+gol_contrario;
this.gol_diferencia=this.gol_a_favor - this.gol_en_contra;
if(gol_equipo>gol_contrario) this.partidos_ganados++;
else if(gol_equipo<gol_contrario) this.partidos_perdidos++;
else this.partidos_empatados++;
this.puntos_totales=this.partidos_ganados*3+this.partidos_empatados;
}
public String GetNombre(){
return this.nombre;
}
public int GetPuntosTotales(){
return this.puntos_totales;
}
public int GetPartidosJugados(){
return this.partidos_jugados;
}
public int GetPartidosGanados(){
return this.partidos_ganados;
}
public int GetPartidosEmpatados(){
return this.partidos_empatados;
}
public int GetPartidosPerdidos(){
return this.partidos_perdidos;
}
public int GetGolDiferencia(){
return this.gol_diferencia;
}
public int GetGolAFavor(){
return this.gol_a_favor;
}
public int GetGolEnContra(){
return this.gol_en_contra;
}
public int compareTo(Equipo other) {
//Criterio 1.- Mayores puntos logrados
if (this.puntos_totales>other.puntos_totales) return(-1);
if (this.puntos_totales<other.puntos_totales) return(1);
//Criterio 2.- Mayores victorias
if (this.partidos_ganados>other.partidos_ganados) return(-1);
if (this.partidos_ganados<other.partidos_ganados) return(1);
//Criterio 3.- Gol diferencia
if (this.gol_diferencia>other.gol_diferencia) return(-1);
if (this.gol_diferencia<other.gol_diferencia) return(1);
//Criterio 4.- Mayores goles anotados
if (this.gol_a_favor>other.gol_a_favor) return(-1);
if (this.gol_a_favor<other.gol_a_favor) return(1);
//Criterio 5.- Menores juegos jugados
if (this.partidos_jugados<other.partidos_jugados) return(-1);
if (this.partidos_jugados>other.partidos_jugados) return(1);
//Criterio 6.- Orden Lexicografico
return (this.nombre.compareToIgnoreCase(other.nombre));
}
public String toString() {
return(this.nombre+" "+Integer.toString(this.puntos_totales)+"p, "+Integer.toString(this.partidos_jugados)+"g "+
"("+Integer.toString(this.partidos_ganados)+"-"+Integer.toString(this.partidos_empatados)+"-"+Integer.toString(this.partidos_perdidos)+"), "+
Integer.toString(this.gol_diferencia)+"gd "+
"("+Integer.toString(this.gol_a_favor)+"-"+Integer.toString(this.gol_en_contra)+")");
}
}
public class Main {
private void parseLine(String line,Equipo[] equipos,int n) {
String line1=new String();
String line2=new String();
String team1=new String();
String team2=new String();
int goles1=0;
int goles2=0;
try {
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\\s*@\\s*");
if (lineScanner.hasNext()) line1=lineScanner.next();
if (lineScanner.hasNext()) line2=lineScanner.next();
Scanner equipoScanner1 = new Scanner(line1);
equipoScanner1.useDelimiter("\\s*#\\s*");
if (equipoScanner1.hasNext()) team1=equipoScanner1.next();
if (equipoScanner1.hasNext()) goles1=equipoScanner1.nextInt(); //Integer.parseInt(lineScanner.next());
Scanner equipoScanner2 = new Scanner(line2);
equipoScanner2.useDelimiter("\\s*#\\s*");
if (equipoScanner2.hasNext()) goles2=equipoScanner2.nextInt(); //Integer.parseInt(lineScanner.next());
if (equipoScanner2.hasNext()) team2=equipoScanner2.next();
if(!team1.equalsIgnoreCase(team2)) {
//Colocar Score del partido para team1
for(int i=0;i<n;i++)
if(team1.equalsIgnoreCase(equipos[i].GetNombre())) equipos[i].SetScore(goles1,goles2);
//Colocar Score del partido para team2
for(int i=0;i<n;i++)
if(team2.equalsIgnoreCase(equipos[i].GetNombre())) equipos[i].SetScore(goles2,goles1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void leerTorneo() {
int ntorneos=0;
int ctorneos=0;
int nequipos=0;
int cequipos=0;
int npartidos=0;
int cpartidos=0;
try {
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
scanner.useDelimiter(System.getProperty("line.separator"));
Equipo[] equipos = new Equipo[255];
if (scanner.hasNext()) ntorneos = Integer.parseInt(scanner.nextLine());
if(ntorneos>0) {
ctorneos = 0;
while (scanner.hasNext() && ctorneos < ntorneos) {
//Lee nombre del Torneo
if (ctorneos>0) System.out.println();
System.out.println(scanner.nextLine());
//Leer numero de equipos del torneo
if (scanner.hasNext()) nequipos = Integer.parseInt(scanner.nextLine());
if(nequipos>1 && nequipos<=30) {
//Leer equipos del torneo
cequipos = 0;
while (scanner.hasNext() && cequipos < nequipos) {
//Lee nombre del equipo y almacenarlo
equipos[cequipos]=new Equipo(scanner.nextLine());
//Siguiente Equipo
cequipos++;
}
}
//Leer numero de partidos del torneo
if (scanner.hasNext()) npartidos = Integer.parseInt(scanner.nextLine());
if(npartidos>0 && npartidos<=1000) {
//Leer partidos del torneo
cpartidos = 0;
while (scanner.hasNext() && cpartidos < npartidos) {
//Lee Score del Partido
String linea=scanner.nextLine();
//Parsear la linea del Score
parseLine(linea,equipos,nequipos);
//Siguiente Partido
cpartidos++;
}
}
//Una vez cargados los Scores Sortear la Lista de Equipos
if (cequipos>0 && cpartidos>0) {
Arrays.sort(equipos,0,cequipos);
//Mostrar el resultado
for(int i=0;i<cequipos;i++) {
System.out.println(Integer.toString(i+1)+") "+equipos[i]);
}
}
//Siguiente Torneo
ctorneos++;
}
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//if (args.length != 1) {
// System.err.println("Usage: java SoccerProblem " + "input file");
// System.exit(0);
//}
Main soc=new Main();
soc.leerTorneo();
System.exit(0);
}
}