Untitled

 avatarfttk
plain_text
2 months ago
3.0 kB
1
Indexable
Never
//rank
import java.util.ArrayList;
import java.util.HashMap;

class UserSolution {   
	
	HashMap<String, Player> players;
	HashMap<String, Problem> problems;
	int totalScore, numPlayer;
	int[] scores;
	int[] failscores;
	
     void init () {
    	players = new HashMap<>();
    	problems = new HashMap<>();
    	totalScore = numPlayer = 0;
    	scores = new int[6005];
    	failscores = new int[6005];
    }

    void destroy () {

    }

    void newPlayer(char name[]) {
    	numPlayer++;
    	String mName=arr2string(name);
    	Player player = new Player(mName,numPlayer);
    	players.put(mName, player);
    }

    void newProblem(char name[], int score) {
    	String mName = arr2string(name);
    	Problem problem = new Problem(mName,score);
    	problems.put(mName, problem);
    	totalScore +=score;
    }

    void changeProblemScore(char name[], int new_score) {
    	String mName=arr2string(name);
    	Problem problem=problems.get(mName);
    	int diff=new_score - problem.score;
    	problem.score = new_score;
    	totalScore +=diff;
    	for(Player p:problem.success) {
    		p.score +=diff;
    		scores[p.id]=p.score;
    	}
    	for(Player p:problem.fail) {
    		p.fail +=diff;
    		failscores[p.id]=p.fail;
    	}
    }

    void attemptProblem(char player_name[], char problem_name[], int attempt_result) {
    	String playerName=arr2string(player_name);
    	String problemName=arr2string(problem_name);
    	
    	Problem problem=problems.get(problemName);
    	Player player = players.get(playerName);
    	if(attempt_result==1) {
    		player.score +=problem.score;
    		problem.success.add(player);
    		scores[player.id]=player.score;
    	}
    	else {
    		player.fail +=problem.score;
    		problem.fail.add(player);
    		failscores[player.id] = player.fail;
    	}
    }

    Result getRank(char player_name[]) {
        Result res = new Result(-1, -1, -1);
        int c=1, b=1, w=numPlayer;
        String name=arr2string(player_name);
        Player player=players.get(name);
        int best=totalScore - player.fail;
        for(int i=0; i<=numPlayer; i++) {
        	if(player.score < scores[i]) c++;
        	if(best < scores[i]) b++;
        	if(scores[player.id] > totalScore - failscores[i]) {
        		w--;
        	}
        	
        }
        res = new Result(c,b,w);
        return res;
    }
    
    String arr2string(char name[]) {
    	StringBuilder s=new StringBuilder();
    	for(int i=0; name[i]!='\0'; i++) {
    		s.append(name[i]);
    	}
    	return s.toString();
    }

}

class Player {
	String name;
	int score, fail, id;
	public Player(String name, int id) {
		this.name=name;
		this.id=id;
		this.score=this.fail=0;
	}
}

class Problem {
	String name;
	int score;
	ArrayList<Player> success, fail;
	public Problem(String name, int score) {
		this.name=name;
		this.score=score;
		this.success=new ArrayList<>();
		this.fail = new ArrayList<>();
	}
}