https://www.acmicpc.net/problem/1202
1202번: 보석 도둑
첫째 줄에 N과 K가 주어진다. (1 ≤ N, K ≤ 300,000) 다음 N개 줄에는 각 보석의 정보 Mi와 Vi가 주어진다. (0 ≤ Mi, Vi ≤ 1,000,000) 다음 K개 줄에는 가방에 담을 수 있는 최대 무게 Ci가 주어진다. (1 ≤ Ci
www.acmicpc.net
분기를 잘못 잡아
디버깅에서 상당히 헤맨 문제였다.
import java.util.*;
import java.io.*;
public class g2_1202 {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
static int n, k;
static long ans = 0L;
static PriorityQueue<jew> priq = new PriorityQueue<>((o1, o2) -> Long.compare(o1.wei, o2.wei));
// 보석을 무게 기준으로 오름차순 정렬
static PriorityQueue<jew> res = new PriorityQueue<>((o1, o2) -> Long.compare(o2.val, o1.val));
// 이후에 나오는 가방에 넣을 수 있는 보석들의 가치 기준 내림차순 정렬
static PriorityQueue<Integer> bag = new PriorityQueue<>();
// 가방의 무게 기준 오름차순 정렬 (여기에 들어갈 수 있는 보석은 이후 모든 가방에 넣을 수 있으므로 res에 삽입)
public static void main(String[] args) {
n = fs.nextInt();
k = fs.nextInt();
for (int i=0;i<n;i++) priq.add(new jew(fs.nextInt(), fs.nextInt()));
for (int i=0;i<k;i++) bag.add(fs.nextInt());
while (!bag.isEmpty()){
Integer curBag = bag.poll();
if (!priq.isEmpty()){
jew curJew = priq.peek();
while (curJew.wei <= curBag){
res.add(priq.poll());
if (priq.isEmpty()) break;
curJew = priq.peek();
}
}
if (!res.isEmpty()) ans += (long)res.poll().val;
}
pw.println(ans);
pw.close();
}
static class jew{
long val, wei;
public jew(long wei, long val){
this.wei = wei;
this.val = val;
}
}
// ----------input function----------
static void sort(int[] a) {
ArrayList<Integer> L = new ArrayList<>();
for (int i : a)
L.add(i);
Collections.sort(L);
for (int i = 0; i < a.length; i++)
a[i] = L.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
'PS > Problems' 카테고리의 다른 글
[Java] 백준 1806번 부분합 - 투 포인터 (0) | 2022.02.06 |
---|---|
[Java] 백준 1208번 부분수열의 합 2 - 투 포인터 / Int, Long 혼용 주의! (0) | 2022.02.06 |
[Java] 백준 1197번 최소 스패닝 트리 (0) | 2022.02.05 |
[Java] 백준 11779번 최소비용 구하기 2 - 다익스트라(nlgn) (0) | 2022.02.04 |
[Java] Div2) C.And Matching - bitwise, case work (0) | 2022.01.28 |