Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- ํ์๊ฐ์
- ๋ถ๋ถ์งํฉ
- ์๋ฃ๊ตฌ์กฐ
- ์นด์นด์ค
- ์ด์งํธ๋ฆฌ
- ํ๋ก์ด๋์์
- PriorityQueue
- ์ธ๊ทธ๋จผํธํธ๋ฆฌ
- ๋ฐฑํธ๋ํน
- ์๋ฎฌ๋ ์ด์
- ์ฌ๊ท
- ์์ด
- BFS
- ํธ๋ผ์ด
- ๋นํธ๋ง์คํน
- ํฌํฌ์ธํฐ
- ๋ค์ต์คํธ๋ผ
- Dijkstra
- ์กฐํฉ
- upperbound
- LowerBound
- ์ด๋ถํ์
- dfs
- dp
- Floyd
- ์ฐ์ ์์ํ
- Union Find
- ๊ตฌํ
- ๊ทธ๋ฆฌ๋
- Django
Archives
- Today
- Total
J
[JAVA] BOJ ๋ฐฑ์ค 1202 ๋ณด์๋๋ ๋ณธ๋ฌธ
ํ์ด
- ์ฒ์ ๋ณด์์ ์ ๋ ฅ ๋ฐ๊ณ compareTo ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋ณด์์ ๊ฐ๋ฒผ์ด ์์ผ๋ก ์ ๋ ฌํ๋ค.
- ์ด๋ ๋ฌด๊ฒ๊ฐ ๊ฐ๋ค๋ฉด ๋ ๊ฐ์น์๋ ๊ฒ์ ์์ ์ค๊ฒ ํ๋ค.
- ๊ฐ๋ฐฉ์ priorityQueue์ ์ ์ฅํ์ฌ ๊ฐ๋ฒผ์ด ์์ผ๋ก ์ ๋ ฌ๋๊ฒ ํ๋ค.
- ๊ฐ๋ฐฉ์ pq์์ ๋ฝ์ ๋ค์ findJewel์ ํด๋น ๊ฐ๋ฐฉ๋ณด๋ค ์์ ๋ณด์๋ง ๋ฃ์ด์ฃผ๋๋ก for๋ฌธ์ ํตํด ๊ฐฑ์ ํด์ค๋ค.
- findJewel์ ๊ฐ์น๊ฐ ๋์ ์์ผ๋ก ์ ๋ ฌ๋๋ค.
- ํด๋น ๊ฐ๋ฐฉ์ ๋ฃ์ ํ๋ณด ๋ณด์์ด ์์ ์์ ์ ์์ผ๋ฏ๋ก findJewel์ด empty์ธ ๊ฒฝ์ฐ continue ์ฒ๋ฆฌ๋ฅผ ํ๋ค.
- pq๋ฅผ ํตํด ๋ฝ์ ๋ณด์์ ๊ทธ ์ค ๊ฐ์ฅ ๊ฐ์น์๋ ๋ณด์์ด๋ฏ๋ก ๋ฐ๋ก result์ ๋ํด์ค๋ค.
์ ์ฒด ์ฝ๋
import java.io.*;
import java.util.*;
public class Main {
static class jewel implements Comparable<jewel>{
int weight;
int value;
public jewel(int weight, int value){
this.weight = weight;
this.value = value;
}
@Override
public int compareTo(jewel jewel) {
int r = Integer.compare(this.weight, jewel.weight);
if(r == 0)
r = -Integer.compare(this.value, jewel.value);
return r;
// ๊ฐ๋ฒผ์ด ๊ฑฐ >> ๋ ๊ฐ์น์๋ ๊ฑฐ
}
}
static PriorityQueue<Long> bags;
static PriorityQueue<jewel> findJewels;
static int K;
public static void main(String[] args) throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
jewel[] jewels = new jewel[N];
findJewels = new PriorityQueue<>((o1, o2) -> -Integer.compare(o1.value, o2.value));
bags = new PriorityQueue<>();
for(int i=0; i<N; i++){
st = new StringTokenizer(bf.readLine(), " ");
jewels[i] = new jewel(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
Arrays.sort(jewels);
for(int i=0; i<K; i++)
bags.offer(Long.parseLong(bf.readLine()));
int i = 0; long result = 0;
while(!bags.isEmpty()){
long b = bags.poll();
for(; i<N; i++){
if(b < jewels[i].weight) break;
findJewels.offer(jewels[i]);
}
if(findJewels.isEmpty()) continue;
jewel j = findJewels.poll();
result += j.value;
}
System.out.println(result);
}
}
'๐ Problem Solving > ๐ BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] BOJ ๋ฐฑ์ค 12837 ๊ฐ๊ณ๋ถ (0) | 2023.08.07 |
---|---|
[JAVA] BOJ ๋ฐฑ์ค 2842 ์ง๋ฐฐ์ ํ์๋ (0) | 2023.07.24 |
[JAVA] BOJ ๋ฐฑ์ค 16472 ๊ณ ๋ฅ์ด (0) | 2023.07.24 |
[JAVA] BOJ ๋ฐฑ์ค 2470 ๋ ์ฉ์ก (0) | 2023.07.07 |
[JAVA] BOJ ๋ฐฑ์ค 14725 ๊ฐ๋ฏธ๊ตด (2) | 2023.05.10 |
Comments