Correct solution is not being accepted for: https://www.codechef.com/problems/UCL


#1

Hi all,

I have tested it several times and it always gives me the required output and I have also set all the other necessary constraints as per the question but still my answer is not being accepted. Please provide me an explanation as to why my answer is not getting accepted so that I can get some sleep. :hot_face::cold_sweat:

Below is my solution:

import java.util.;
import java.lang.
;
import java.io.*;

class Team{
private String teamName;
private int matchNumber;
private int goalsScored;
private int goalsReceived;
private int goalDiff;
private int points;

public Team(String teamName, int goalsScored, int goalsReceived ) {
	this.teamName = teamName;
	this.goalsScored = goalsScored;
	this.goalsReceived=goalsReceived;
}

public String getTeamName() {
	return teamName;
}

public Team getTeam(String teamName) {
	
	if(this.teamName.equals(teamName)) {
	return this;
	}
	return null;
}

public void setTeamName(String teamName) {
	this.teamName = teamName;
}

public int getMatchNumber() {
	return matchNumber;
}

public void setMatchNumber(int matchNumber) {
	this.matchNumber = matchNumber;
}

public int getGoalsScored() {
	return goalsScored;
}

public void setGoalsScored(int goalsScored) {
	this.goalsScored = goalsScored;
}

public int getGoalsReceived() {
	return goalsReceived;
}

public void setGoalsReceived(int goalsReceived) {
	this.goalsReceived = goalsReceived;
}

public int getGoalDiff() {

	return goalDiff;
}

public void setGoalDiff(int goalsScored, int goalsReceived) {
	   goalDiff= (this.goalDiff+goalsScored) - goalsReceived;

// if(goalDiff<0) {
// goalDiff = -goalDiff;
// }
this.goalDiff = goalDiff;
}

public int getPoints() {
	return points;
}

public void setPoints(int points) {
	this.points = points;
}

public boolean isHigherRanked(Team team) {
	if(this.getPoints()>team.getPoints()) {
		return true;
	}else if(this.getPoints()==team.getPoints()) {
		return this.goalDiff > team.goalDiff;
	}
	else {
		return this.getPoints()>team.getPoints();
	}
}

public Team getHigherRankedTeam(Team team) {
	if(this.getPoints()>team.getPoints()) {
		return this;
	}else if(this.getPoints()==team.getPoints() && this.goalDiff>team.goalDiff) {
		return this;
	}
	else {
		return team;
	}
}

@Override
public boolean equals(Object obj) {
	if(!(obj instanceof Team)) {
		return false;
	}else {
		Team team = (Team) obj;
		return this.teamName.equals(team.teamName);
	}
}
@Override
public int hashCode() {
	// TODO Auto-generated method stub
	return teamName.hashCode();
}

@Override
public String toString() {
	// TODO Auto-generated method stub
	return "TeamName: "+teamName+", GoalsScored: "+goalsScored+", Points: "+points+", goalDiff: "+goalDiff;
}

}

class Codechef
{
public static void main (String[] args) throws java.lang.Exception{

	LinkedHashMap<String, Team> map = new LinkedHashMap<>();
	LinkedHashMap<String, Team> map2 = new LinkedHashMap<>();
//	List<Team> 	list = new ArrayList<>();
	//list.add(new Team("dsda", 1));
//	System.out.println(list);
	
	Team homeTeamRef;
	Team awayTeamRef;
	
	Scanner scanner = new Scanner(System.in);
	int testCases = scanner.nextInt();
	
	if(testCases>=1 && testCases<=50) {
		int i=1;
	while(i<=testCases*12) {
	
        String homeTeam = scanner.next();
		int homeGoals = scanner.nextInt();
		scanner.next();
		int awayGoals = scanner.nextInt();
		
		String awayTeam = scanner.next();
		
		if((homeTeam.length()<=10 && homeTeam.equals(homeTeam.toLowerCase()))
				&& ((awayTeam.length()<=10 &&  awayTeam.equals(awayTeam.toLowerCase())))
				&& (homeGoals>=0 && homeGoals<=100)
				&& (awayGoals>=0 && homeGoals<=100)) {
          
			if(i<=12) {
	    	if(map.containsKey(homeTeam)) {
	    		 homeTeamRef =map.get(homeTeam);
	    		 homeTeamRef.setGoalsScored(homeGoals);
	    		 homeTeamRef.setGoalsReceived(awayGoals); 
	    		 homeTeamRef.setGoalDiff(homeTeamRef.getGoalsScored(), homeTeamRef.getGoalsReceived());
              }else {
            	  homeTeamRef = new Team(homeTeam, homeGoals, awayGoals);
            		 homeTeamRef.setGoalsScored(homeGoals);
		    		 homeTeamRef.setGoalsReceived(awayGoals); 
		    		 homeTeamRef.setGoalDiff(homeTeamRef.getGoalsScored(), homeTeamRef.getGoalsReceived());
            	 
              }
	    	
	    	if(map.containsKey(awayTeam)) {
	    		awayTeamRef =map.get(awayTeam);
	    		awayTeamRef.setGoalsScored(awayGoals);
	    		awayTeamRef.setGoalsReceived(homeGoals);
	    		 awayTeamRef.setGoalDiff(awayTeamRef.getGoalsScored(), awayTeamRef.getGoalsReceived());
             }else {
            	 awayTeamRef = new Team(awayTeam, awayGoals, homeGoals);
         		awayTeamRef.setGoalsScored(awayGoals);
	    		awayTeamRef.setGoalsReceived(homeGoals);
	    		 awayTeamRef.setGoalDiff(awayTeamRef.getGoalsScored(), awayTeamRef.getGoalsReceived());
	    	
             }
			
			int homeTeamPoints = homeTeamRef.getPoints();
			int awayTeamPoints= awayTeamRef.getPoints();
			if(homeGoals>awayGoals) {
				homeTeamRef.setPoints(homeTeamPoints+3);
			}else if(homeGoals<awayGoals) {
				awayTeamRef.setPoints(awayTeamPoints+3);
			}if(awayGoals==homeGoals) {
				homeTeamRef.setPoints(homeTeamPoints+1);
				awayTeamRef.setPoints(awayTeamPoints+1);
			}
			 map.put(homeTeam, homeTeamRef);
       	  map.put(awayTeam, awayTeamRef);
		}
		else {
			if(map2.containsKey(homeTeam)) {
	    		 homeTeamRef =map2.get(homeTeam);
	    		 homeTeamRef.setGoalsScored(homeGoals);
	    		 homeTeamRef.setGoalsReceived(awayGoals); 
	    		 homeTeamRef.setGoalDiff(homeTeamRef.getGoalsScored(), homeTeamRef.getGoalsReceived());
             }else {
           	  homeTeamRef = new Team(homeTeam, homeGoals, awayGoals);
           		 homeTeamRef.setGoalsScored(homeGoals);
	    		 homeTeamRef.setGoalsReceived(awayGoals); 
	    		 homeTeamRef.setGoalDiff(homeTeamRef.getGoalsScored(), homeTeamRef.getGoalsReceived());
           	 
             }
	    	
	    	if(map2.containsKey(awayTeam)) {
	    		awayTeamRef =map2.get(awayTeam);
	    		awayTeamRef.setGoalsScored(awayGoals);
	    		awayTeamRef.setGoalsReceived(homeGoals);
	    		 awayTeamRef.setGoalDiff(awayTeamRef.getGoalsScored(), awayTeamRef.getGoalsReceived());
            }else {
           	 awayTeamRef = new Team(awayTeam, awayGoals, homeGoals);
        		awayTeamRef.setGoalsScored(awayGoals);
	    		awayTeamRef.setGoalsReceived(homeGoals);
	    		 awayTeamRef.setGoalDiff(awayTeamRef.getGoalsScored(), awayTeamRef.getGoalsReceived());
	    	
            }
			
			int homeTeamPoints = homeTeamRef.getPoints();
			int awayTeamPoints= awayTeamRef.getPoints();
			if(homeGoals>awayGoals) {
				homeTeamRef.setPoints(homeTeamPoints+3);
			}else if(homeGoals<awayGoals) {
				awayTeamRef.setPoints(awayTeamPoints+3);
			}if(awayGoals==homeGoals) {
				homeTeamRef.setPoints(homeTeamPoints+1);
				awayTeamRef.setPoints(awayTeamPoints+1);
			}
			 map2.put(homeTeam, homeTeamRef);
      	  map2.put(awayTeam, awayTeamRef);
		
			
		}
	}

// if(i==24 && scanner.hasNextLine()) {
// System.out.println(map);
// System.out.println(map2);
// System.out.println(i);
// // i=25;
// // break;
// }
i++;
}

//	System.out.println(map2);	
	
	  
			System.out.print(getHighestRankedTeam(map)+"\n"+getHighestRankedTeam(map2));	
			//  scanner.nextLine();

// try {
// int read = System.in.read(new byte[2]);
// System.out.print(getHighestRankedTeam(map2));
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// scanner.close();
// scanner=null;

   }
	

}

//
private static String getHighestRankedTeam(LinkedHashMap<String, Team> map) {
List list = new ArrayList<>();
int i=0;
//
for (Map.Entry<String,Team> entry : map.entrySet())
{
list.add(entry.getValue());
}
//
String highestRankTeam = null;

	  for(int j=0; j<list.size(); j++) {
		Team jTeam = list.get(j);
		
		  for(int k=list.size()-1; k>0; k--) {
			  Team kTeam = list.get(k);
               
			  if(highestRankTeam!=null) {
				Team hTeam=map.get(highestRankTeam);
				highestRankTeam=jTeam.getHigherRankedTeam(kTeam).getTeamName();
				Team hTeam2=map.get(highestRankTeam);
				highestRankTeam=hTeam.getHigherRankedTeam(hTeam2).getTeamName();
			  }
			  else {
			 highestRankTeam=jTeam.getHigherRankedTeam(kTeam).getTeamName();
			  }
		  }
		  
			  
		  
		  
		  break;
	  }   
	  LinkedHashMap<String, Team> map1 = new LinkedHashMap<>();
	  map1.putAll(map);
	  map1.remove(highestRankTeam);
	  getSecondHighestRankedTeam(map1);
	  
	  return highestRankTeam+" "+getSecondHighestRankedTeam(map1);
}


private static String getSecondHighestRankedTeam(LinkedHashMap<String, Team> map) {
	List<Team> list = new ArrayList<>();
	int i=0;

//
for (Map.Entry<String,Team> entry : map.entrySet())
{
list.add(entry.getValue());
}
//
String secondHighestRankTeam = null;

	  for(int j=0; j<list.size(); j++) {
		Team jTeam = list.get(j);
		
		  for(int k=list.size()-1; k>0; k--) {
			  Team kTeam = list.get(k);
               
			  if(secondHighestRankTeam!=null) {
				Team hTeam=map.get(secondHighestRankTeam);
				secondHighestRankTeam=jTeam.getHigherRankedTeam(kTeam).getTeamName();
				Team hTeam2=map.get(secondHighestRankTeam);
				secondHighestRankTeam=hTeam.getHigherRankedTeam(hTeam2).getTeamName();
			  }
			  else {
				  secondHighestRankTeam=jTeam.getHigherRankedTeam(kTeam).getTeamName();
			  }
		  }
		  
			  
		  
		  
		  break;
	  }   
	  
	  
	  return secondHighestRankTeam;
}

}