• 문제 링크
1487번: 물건 팔기
첫째 줄에 최대 이익을 만들어주는 가격을 출력한다. 이익이 최대인 가격이 여러개라면, 가장 낮은 가격을 출력한다. 또, 어떤 가격으로 팔아도 이익을 남길 수 없다면 0을 출력한다.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read();
Pair[] arr = new Pair[n];
Set<Integer> set = new TreeSet<>();
while (n-- > 0) set.add((arr[n] = new Pair(read(), read())).in);
int max = 0, res = 0;
for (int in : set) {
int sum = 0;
for (Pair p : arr) if (in <= p.in && in > p.out) sum += in - p.out;
if (max < sum) {
max = sum;
res = in;
}
}
bw.write(String.valueOf(res));
bw.flush();
}
private static class Pair {
int in, out;
Pair(int in, int out) {
this.in = in;
this.out = out;
}
}
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' 카테고리의 다른 글
[백준] 20170 Commemorative Dice - Brute Force / Java (0) | 2023.10.30 |
---|---|
[백준] 20651 Daisy Chains - Brute Force / Java (0) | 2023.10.29 |
[백준] 9881 Ski Course Design - Brute Force / Java (0) | 2023.10.27 |
[백준] 14626 ISBN - Brute Force / Java (0) | 2023.10.26 |
[백준] 1359 복권 - Brute Force / Java (0) | 2023.10.25 |
댓글