https://www.acmicpc.net/problem/2238
클래스를 만들고 첫 입찰자 이름을 같이 넣어 놓는다.
O(1)로 찾기 위해 price를 key로 hashmap에 넣는다.
price는 또한 정렬 기준이므로 클래스에도 넣는다.
1. 사람 수 내림차순 정렬
2. 가격 순 오름차순 정렬
문제에서 경매자는 반드시 존재하므로 optinal을 get()한다.
입력이 U 이하인지, 유효한 입찰로 간주하는게 U 이하인지 헷갈렸는데, 전자인듯 하다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
class Main {
static class P {
int cnt = 1, price;
String first;
P(int price, String first) {
this.price = price;
this.first = first;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int U = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
Map<Integer, P> m = new HashMap<>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
String name = st.nextToken();
int price = Integer.parseInt(st.nextToken());
if (m.containsKey(price)) {
m.get(price).cnt++;
} else {
m.put(price, new P(price, name));
}
}
P ans = m.values()
.stream()
.sorted(Comparator.comparing((P p) -> p.cnt)
.thenComparing(p -> p.price))
.findFirst()
.get();
System.out.println(String.format("%s %d", ans.first, ans.price));
}
}
'[백준] PS > Java [실랜디]' 카테고리의 다른 글
[백준 2303] 숫자 게임 - JAVA (0) | 2025.03.09 |
---|---|
[백준 2134] 창고 이전 - JAVA (0) | 2025.03.09 |
[백준 1972] 놀라운 문자열 - JAVA (0) | 2025.03.07 |
[백준 2012] 등수 매기기 - JAVA (1) | 2025.03.07 |
[백준 1940] 주몽 - JAVA (1) | 2025.03.06 |