๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 2817_ALPS์‹ ํˆฌํ‘œ

๋ฟŒ์•ผ._. 2024. 7. 18. 21:40
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/2817)

< ALPS์‹ ํˆฌํ‘œ >

 

๋ฌธ์ œ ํ’€์ด 

 

์ „์ฒด ์Šคํƒœํ”„ ์ˆ˜์˜ 5% ์ด์ƒ ํ‘œ๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ๋“ค์˜ ํ‘œ๋ฅผ 1๋ถ€ํ„ฐ 14๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ์ •๋ ฌํ•˜์—ฌ ๋‹ต์„ ๊ตฌํ•œ๋‹ค.

๋‹ต์„ ์ถœ๋ ฅํ•  ๋•Œ๋Š” 5% ์ด์ƒ ํ‘œ๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ ์ค‘์—์„œ ์นฉ์„ 0๊ฐœ ๋ฐ›์•˜๋”๋ผ๋„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ฆ‰, 5% ์ด์ƒ ํ‘œ๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์€ ๋ชจ๋‘ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;

public class _2817_ { // ALPS์‹ ํˆฌํ‘œ
	static class Participant implements Comparable<Participant> {
		private String name;
		private double value;

		public Participant(String name, double value) {
			this.name = name;
			this.value = value;
		}

		@Override
		public int compareTo(Participant o) {
			return Double.compare(o.value, this.value);
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;

		int X = Integer.parseInt(bf.readLine());
		int N = Integer.parseInt(bf.readLine());

		ArrayList<Participant> list = new ArrayList<>();
		HashMap<String, Integer> map = new HashMap<>();
		for (int i = 0; i < N; i++) {
			st = new StringTokenizer(bf.readLine());

			String name = st.nextToken();
			int value = Integer.parseInt(st.nextToken());

			if (value < X * 0.05) {
				continue;
			}
			map.put(name, 0);
			for (int j = 1; j <= 14; j++) {
				list.add(new Participant(name, (double) value / j));
			}
		}
		Collections.sort(list);

		for (int i = 0; i < 14; i++) {
			Participant participant = list.get(i);
			if (map.containsKey(participant.name)) {
				map.replace(participant.name, map.get(participant.name) + 1);
			}
		}

		ArrayList<String> keySet = new ArrayList<>(map.keySet());
		Collections.sort(keySet);
		for (String name : keySet) {
			bw.write(name + " " + map.get(name) + "\n");
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
X, N : ์ฐธ๊ฐ€์ž๋“ค์˜ ์ˆ˜, ์Šคํƒœํ”„์˜ ์ˆ˜
list : Participant๋ฅผ value๋กœ ๊ฐ€์ง€๋Š” ArrayList
map : <String, Integer> HashMap, ์Šคํƒœํ”„๋ณ„ ์–ป์€ ์นฉ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ HashMap
name, value : ์Šคํƒœํ”„ ์ด๋ฆ„, ๋“ํ‘œ์ˆ˜

 

Participant

์Šคํƒœํ”„ ์ด๋ฆ„ name๊ณผ ๋“ํ‘œ์ˆ˜๋ฅผ 1๋ถ€ํ„ฐ 14๋กœ ๋‚˜๋ˆˆ ๊ฐ’์ธ value๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๋ฉฐ value๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.

 

Main

์ฐธ๊ฐ€์ž๋“ค์˜ ์ˆ˜ X์™€ ์Šคํƒœํ”„์˜ ์ˆ˜ N์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์Šคํƒœํ”„ ์ˆ˜๋งŒํผ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

1) ์Šคํƒœํ”„์˜ ์ •๋ณด - ์Šคํƒœํ”„์˜ ์ด๋ฆ„ name๊ณผ ๋“ํ‘œ์ˆ˜ value๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

2) ๋“ํ‘œ์ˆ˜๊ฐ€ (์ฐธ๊ฐ€์ž์˜ ์ˆ˜ * 0.05) ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ํ›„๋ณด์—์„œ ์ œ์™ธํ•œ๋‹ค.

3) ๋“ํ‘œ์ˆ˜๊ฐ€ (์ฐธ๊ฐ€์ž์˜ ์ˆ˜ * 0.05) ์ด์ƒ์ด๋ฉด map์— <์Šคํƒœํ”„ ์ด๋ฆ„, 0>์„ ์ €์žฅํ•œ๋‹ค. list์— [์Šคํƒœํ”„ ์ด๋ฆ„, ๋“ํ‘œ์ˆ˜๋ฅผ 1๋ถ€ํ„ฐ 14๊นŒ์ง€ ๋‚˜๋ˆˆ ์ˆ˜]๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๋Š” Participant๋ฅผ ์ €์žฅํ•œ๋‹ค.

 

list๋ฅผ ์ •๋ ฌํ•œ ํ›„ list์˜ 0๋ฒˆ์งธ ๊ฐ’๋ถ€ํ„ฐ 13๋ฒˆ์งธ๊นŒ์ง€ ์Šคํƒœํ”„์˜ ์ด๋ฆ„์„ ํ™•์ธ ํ›„ map์—์„œ ์ฐพ์•„ value๋ฅผ 1 ๋”ํ•œ๋‹ค. ์ตœ์ข… map์˜ key๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ key๊ณผ value๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.