https://www.acmicpc.net/problem/17503
17503번: 맥주 축제
첫 번째 줄에 축제가 열리는 기간 N (1 ≤ N ≤ 200,000) 과, 채워야 하는 선호도의 합 M (1 ≤ M < 231) 과, 마실 수 있는 맥주 종류의 수 K (N ≤ K ≤ 200,000) 가 주어집니다. 다음 K개의 줄에는 1번부터 K
www.acmicpc.net
package BOJ.greedy;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_17503 {
static class Beer{
int value;
int limit;
public Beer(int value, int limit) {
this.value = value;
this.limit = limit;
}
@Override
public String toString() {
return "Beer{" +
"value=" + value +
", limit=" + limit +
'}';
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int m = stoi.apply(st.nextToken());
int k = stoi.apply(st.nextToken());
Beer[] beers = new Beer[k];
for(int i = 0 ; i < k ; i++){
st = new StringTokenizer(br.readLine());
int value = stoi.apply(st.nextToken());
int limit = stoi.apply(st.nextToken());
beers[i] = new Beer(value,limit);
}
Arrays.sort(beers,(o1,o2)->{
if(o1.limit == o2.limit){
return o2.value - o1.value;
}
return o1.limit - o2.limit;
});
// System.out.println(Arrays.toString(beers));
PriorityQueue<Beer> pq = new PriorityQueue<>((o1,o2)->{
return o1.value - o2.value;
});
long value = 0;
long limit = 0;
int index = 0;
for( ; index < Math.min(n,k) ; index++){
pq.offer(beers[index]);
value += beers[index].value;
limit = Math.max(limit, beers[index].limit);
// System.out.println(index+" "+value);
}
if(value >= m){
System.out.println(limit);
return;
}
while(index < k){
Beer temp = pq.poll();
value -= temp.value;
pq.offer(beers[index]);
value += beers[index].value;
limit = Math.max(limit, beers[index].limit);
// System.out.println(index+" "+value);
index++;
if(value >= m){
System.out.println(limit);
return;
}
}
System.out.println(-1);
}
}
'알고리즘' 카테고리의 다른 글
백준 17391번 무한 부스터 (JAVA) (1) | 2023.01.25 |
---|---|
프로그래머스 직각삼각형 출력하기 (JAVA) (0) | 2023.01.24 |
백준 14430번 자원 캐기 (JAVA) (0) | 2023.01.22 |
프로그래머스 유한소수 판별하기 (JAVA) (0) | 2023.01.21 |
백준 13702번 이상한 술집 (JAVA) (0) | 2023.01.20 |