View

๋ฐ˜์‘ํ˜•

๐Ÿ’ก ๋ฌธ์ œ๋ณด๊ธฐ

https://www.acmicpc.net/problem/10814

 

๐Ÿ’กํ’€์ด๋ฐฉ์‹

์šฐ์„ ์ˆœ์œ„ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์ด 

๋‚˜์ด๊ฐ€ ๋™์ผํ•  ๋•Œ๋Š” ๋จผ์ € ๊ฐ€์ž…ํ•œ ์ˆœ์œผ๋กœ ํ•˜๊ธฐ ์œ„ํ•ด index๋ฅผ ๋ถ€์—ฌํ•จ

	@Override
		public int compareTo(Node o) {
			if (o.age - this.age == 0) {// ๋‚˜์ด๊ฐ€ ๋™์ผํ•  ๊ฒฝ์šฐ
				// ๋จผ์ €๊ฐ€์ž…ํ•œ ์•  ๋จผ์ € ์ถœ๋ ฅ
				return this.idx - o.idx;
			} else {
				return this.age - o.age;
			}

		}

 

 

๐Ÿ’ก ์ตœ์ข…์ฝ”๋“œ

package ๋ฐฑ์ค€;

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

public class Main_10814 {

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

		PriorityQueue arr = new PriorityQueue<>();

		for (int i = 0; i < N; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int age = Integer.parseInt(st.nextToken());
			String name = st.nextToken();
			arr.add(new Node(i, age, name));
		}

		for (int i = 0; i < N; i++) {
			Node node = (Node) arr.poll();
			System.out.println(node.age + " " + node.name);
		}
	}

	static class Node implements Comparable<Node> {
		int idx;
		int age;
		String name;

		public Node(int idx, int age, String name) {
			this.idx = idx;
			this.age = age;
			this.name = name;
		}

		@Override
		public int compareTo(Node o) {
			if (o.age - this.age == 0) {// ๋‚˜์ด๊ฐ€ ๋™์ผํ•  ๊ฒฝ์šฐ
				// ๋จผ์ €๊ฐ€์ž…ํ•œ ์•  ๋จผ์ € ์ถœ๋ ฅ
				return this.idx - o.idx;
			} else {
				return this.age - o.age;
			}

		}
	}

}
๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31