240511 KCPC

Jongleee·2024년 5월 13일
0

TIL

목록 보기
570/737
private static final String NEW_LINE = "\n";

private static class Team implements Comparable<Team> {
	int idx;
	int time;
	int report;
	int score;

	public Team(int idx, int time, int report, int score) {
		this.idx = idx;
		this.time = time;
		this.report = report;
		this.score = score;
	}

	@Override
	public int compareTo(Team s) {
		if (this.score != s.score) {
			return Integer.compare(s.score, this.score);
		}
		if (this.report != s.report) {
			return Integer.compare(this.report, s.report);
		}
		return Integer.compare(this.time, s.time);
	}
}

public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int t = Integer.parseInt(br.readLine());

	StringBuilder sb = new StringBuilder();

	while (t-- > 0) {
		processTestCase(br, sb);
	}

	System.out.print(sb.toString());
}

private static void processTestCase(BufferedReader br, StringBuilder sb) throws IOException {
	StringTokenizer st = new StringTokenizer(br.readLine());
	int number = Integer.parseInt(st.nextToken());
	int problem = Integer.parseInt(st.nextToken());
	int id = Integer.parseInt(st.nextToken()) - 1;
	int entry = Integer.parseInt(st.nextToken());

	Team[][] teams = initializeTeams(number, problem);

	int time = 1;
	while (entry-- > 0) {
		updateTeamData(br, teams, time);
		time++;
	}

	PriorityQueue<Team> pq = calculateTeamScores(teams, number, problem);

	int count = findTeamRank(id, pq);

	sb.append(count).append(NEW_LINE);
}

private static Team[][] initializeTeams(int number, int problem) {
	Team[][] teams = new Team[number][problem];
	for (int i = 0; i < number; i++) {
		for (int j = 0; j < problem; j++) {
			teams[i][j] = new Team(0, 0, 0, 0);
		}
	}
	return teams;
}

private static void updateTeamData(BufferedReader br, Team[][] teams, int time) throws IOException {
	StringTokenizer st = new StringTokenizer(br.readLine());
	int i = Integer.parseInt(st.nextToken()) - 1;
	int j = Integer.parseInt(st.nextToken()) - 1;
	int s = Integer.parseInt(st.nextToken());

	teams[i][j].idx = i;
	teams[i][j].score = Math.max(teams[i][j].score, s);
	teams[i][j].time = time;
	teams[i][j].report++;
}

private static PriorityQueue<Team> calculateTeamScores(Team[][] teams, int number, int problem) {
	PriorityQueue<Team> pq = new PriorityQueue<>();
	for (int i = 0; i < number; i++) {
		Team s = new Team(i, 0, 0, 0);
		for (int j = 0; j < problem; j++) {
			s.score += teams[i][j].score;
			s.time = Math.max(s.time, teams[i][j].time);
			s.report += teams[i][j].report;
		}
		pq.offer(s);
	}
	return pq;
}

private static int findTeamRank(int id, PriorityQueue<Team> pq) {
	int count = 1;
	while (!pq.isEmpty()) {
		Team current = pq.poll();
		if (current.idx == id) {
			break;
		}
		count++;
	}
	return count;
}

출처:https://www.acmicpc.net/problem/3758

0개의 댓글