View
[๋ฐฑ์ค] 10814: ๋์ด์ ์ ๋ ฌ(Java)
์ฑ ์ฝ๋ ๊ฐ์ 2022. 10. 3. 13:44๋ฐ์ํ
๐ก ๋ฌธ์ ๋ณด๊ธฐ
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;
}
}
}
}
๋ฐ์ํ
'์๊ณ ๋ฆฌ์ฆ > ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ณดํธ์์์ ์ค์ฑํํ ๋๋ฌผ (0) | 2022.10.06 |
---|---|
[๋ฐฑ์ค] 2630: ์์ข ์ด ๋ง๋ค๊ธฐ(๋ถํ ์ ๋ณต) (0) | 2022.10.03 |
[๋ฐฑ์ค_์คํจ] (0) | 2022.10.02 |
[SWEA]1249: ๋ณด๊ธ๋ก D4(Java) (0) | 2022.10.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ฃผํ์ง ๋ชปํ ์ ์(Java) (0) | 2022.10.01 |
reply