๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 5462_POI

๋ฟŒ์•ผ._. 2024. 11. 26. 21:51
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/5462)

< POI >

 

๋ฌธ์ œ ํ’€์ด 

 

: ๊ฐ ๋ฌธ์ œ ์ ์ˆ˜

: ๊ฐ ์ฐธ๊ฐ€์ž๋ณ„ ํš๋“ ์ ์ˆ˜

: ๊ฐ ์ฐธ๊ฐ€์ž๋ณ„ ํ‘ผ ๋ฌธ์ œ ์ˆ˜

๋ฅผ ๊ตฌํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•ด ์ ์ˆ˜ > ํ‘ผ ๋ฌธ์ œ ์ˆ˜ > ID ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class _5462_ { // POI
	static class Person implements Comparable<Person> {
		private int score;
		private int problem;
		private int id;
        
		public Person(int score, int problem, int id) {
			this.score = score;
			this.problem = problem;
			this.id = id;
		}
        
		@Override
		public int compareTo(Person o) {
			if (this.score == o.score) {
				if (this.problem == o.problem) {
					return this.id - o.id;
				}
				return o.problem - this.problem;
			}
			return o.score - this.score;
		}
	}
    
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
        
		int N = Integer.parseInt(st.nextToken());
		int T = Integer.parseInt(st.nextToken());
		int P = Integer.parseInt(st.nextToken());
        
		int arr[][] = new int[N][T];
		int score[] = new int[T];
		for (int i = 0; i < N; i++) {
			st = new StringTokenizer(bf.readLine());
			for (int j = 0; j < T; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
				if (arr[i][j] == 0) {
					score[j] += 1;
				}
			}
		}
        
		PriorityQueue<Person> queue = new PriorityQueue<>();
        
		for (int i = 0; i < N; i++) {
			int num = 0, s = 0;
			for (int j = 0; j < T; j++) {
				if (arr[i][j] == 1) {
					num += 1;
					s += score[j];
				}
			}
			queue.add(new Person(s, num, i + 1));
		}
        
		int rank = 0;
		Person person = null;
		while (!queue.isEmpty()) {
			person = queue.poll();
			if (person.id == P) {
				rank += 1;
				break;
			}
			rank += 1;
		}
		System.out.println((person.score) + " " + rank);
	}
}
๋ณ€์ˆ˜)
N, T, P : ์ฐธ๊ฐ€์ž ์ˆ˜, ๋ฌธ์ œ ์ˆ˜, P ID
arr : ๋ฌธ์ œ ํ‘ผ ์—ฌ๋ถ€
score : ๊ฐ ๋ฌธ์ œ ์ ์ˆ˜
queue : ์šฐ์„ ์ˆœ์œ„ ํ
rank : P์˜ ๋“ฑ์ˆ˜

 

Person

์ ์ˆ˜, ํ‘ผ ๋ฌธ์ œ ์ˆ˜๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง

์ ์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ, ํ‘ผ ๋ฌธ์ œ ์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ, ID ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•œ๋‹ค.

 

Main

์ฐธ๊ฐ€์ž ์ˆ˜, ๋ฌธ์ œ ์ˆ˜, P ID๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๊ฐ ์ฐธ๊ฐ€์ž์˜ ๋ฌธ์ œ ํ‘ผ ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅํ•˜๋ฉฐ ๊ฐ ๋ฌธ์ œ๋ฅผ ํ’€์ง€ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด score์— ์ €์žฅํ•œ๋‹ค. arr์„ ์ „์ฒด ํƒ์ƒ‰ํ•˜๋ฉฐ ๊ฐ ์ฐธ๊ฐ€์ž๋ณ„ ์ ์ˆ˜์™€ ํ‘ผ ๋ฌธ์ œ ์ˆ˜๋ฅผ ๊ตฌํ•ด ์šฐ์„ ์ˆœ์œ„ ํ์— Person ๊ฐ์ฒด๋กœ ์ €์žฅํ•œ๋‹ค. ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ poll ํ•˜๋ฉด์„œ P๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ rank๋ฅผ ๊ตฌํ•œ๋‹ค. ์ตœ์ข… P์˜ ์ ์ˆ˜์™€ rank๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 6147_Bookshelf  (0) 2024.12.02
[Baekjoon] 9047_6174  (1) 2024.11.29
[Baekjoon] 6191_Cows on Skates  (0) 2024.11.25
[Baekjoon] 6832_Maze  (0) 2024.11.22
[Baekjoon] 14145_ลฝetva  (0) 2024.11.21