• 문제 링크
14471번: 포인트 카드
예제 입출력 1에서, 포인트 카드 1의 꽝 도장 3개와 포인트 카드 3의 꽝 도장 1개를 당첨 도장으로 바꾸면, 4엔으로 5-1=4장의 카드가 경품과 교환 가능하게 되어, 이것이 최소 비용이다. 예제 입출
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), m = read();
Pair[] arr = new Pair[m];
for (int i = 0; i < m; i++) arr[i] = new Pair(read(), read());
Arrays.sort(arr, new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
return o2.b - o1.b;
}
});
int cnt = 0;
while (m-- > 1) if (arr[m].b > n) cnt += n - arr[m].a;
bw.write(String.valueOf(cnt));
bw.flush();
}
private static class Pair {
int a, b;
Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 29767 점수를 최대로 - Greedy / Java (0) | 2023.11.21 |
---|---|
[백준] 30457 단체줄넘기 - Greedy / Java (0) | 2023.11.20 |
[백준] 21313 문어 - Greedy / Java (0) | 2023.11.19 |
[백준] 6125 Treasure Cave - Graph Theory / Java (0) | 2023.11.17 |
[백준] 11322 Numbers are Easy - Graph Theory / Java (0) | 2023.11.16 |
댓글