π·πΌλͺ¨μ¬λ΄μ κ°λ°μμ²π·πΌ
[JAVA/μ½λ©ν μ€νΈ] μ΅λ μμ μ€μΌμ₯΄(PriorityQueue μμ©λ¬Έμ ) λ³Έλ¬Έ
κ°λ°/μκ³ λ¦¬μ¦ λ¬Έμ νμ΄ JAVA
[JAVA/μ½λ©ν μ€νΈ] μ΅λ μμ μ€μΌμ₯΄(PriorityQueue μμ©λ¬Έμ )
μμΌμ΄ 2022. 2. 15. 23:39λ°μν
μ€λͺ
νμλ μ λͺ ν κ°μ°μμ΄λ€. Nκ°μ΄ κΈ°μ μμ κ°μ° μμ²μ ν΄μλ€. κ° κΈ°μ μ DμΌ μμ μμ κ°μ°μ ν΄ μ£Όλ©΄ Mλ§νΌμ κ°μ°λ£λ₯Ό μ£ΌκΈ°λ‘ νλ€.
κ° κΈ°μ μ΄ μμ²ν Dμ Mλ₯Ό λ°νμΌλ‘ κ°μ₯ λ§μ λμ λ² μ μλλ‘ κ°μ° μ€μΌμ₯΄μ μ§μΌ νλ€.
λ¨ κ°μ°μ νΉμ±μ νμλ ν루μ νλμ κΈ°μ μμλ§ κ°μ°μ ν μ μλ€.
μ λ ₯
첫 λ²μ§Έ μ€μ μμ°μ N(1<=N<=10,000)μ΄ μ£Όμ΄μ§κ³ , λ€μ Nκ°μ μ€μ M(1<=M<=10,000)κ³Ό D(1<=D<=10,000)κ° μ°¨λ‘λ‘ μ£Όμ΄μ§λ€.
μΆλ ₯
첫 λ²μ§Έ μ€μ μ΅λλ‘ λ² μ μλ μμ μ μΆλ ₯νλ€.
μμ μ λ ₯ 1
6
50 2
20 1
40 2
60 3
30 3
30 1
μμ μΆλ ₯ 1
150
import java.util.*;
class Time implements Comparable<Time>{
public int money;
public int day;
Time(int money, int day){
this.money = money;
this.day = day;
}
@Override
public int compareTo(Time o){
if(this.day == o.day){
return o.money - this.money;
}else{
return o.day - this.day;
}
}
}
public class Main {
public int solution(ArrayList<Time> time, int max) {
int result = 0;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
int i=0;
for(int j=max; j>=1; j--){
for( ; i<time.size(); i++){
if(time.get(i).day<j){
break;
}else{
pq.offer(time.get(i).money);
}
}
// μμΌλ©΄ λ°νμ μλ¬
if(!pq.isEmpty()){
result += pq.poll();
}
}
return result;
}
public static void main(String[] args) {
Main main = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int max = 0;
ArrayList<Time> time = new ArrayList<>();
for(int i=0; i<n; i++){
Time tmp = new Time(kb.nextInt(), kb.nextInt());
time.add(tmp);
if(max<tmp.day){
max = tmp.day;
}
}
Collections.sort(time);
System.out.println(main.solution(time, max));
}
}
'κ°λ° > μκ³ λ¦¬μ¦ λ¬Έμ νμ΄ JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[νλ‘κ·Έλλ¨Έμ€/JAVA] κ°μ₯ ν° μ (0) | 2022.02.18 |
---|---|
[νλ‘κ·Έλλ¨Έμ€/JAVA] κΈ°μ§κ΅ μ€μΉ (0) | 2022.02.17 |
[JAVA/μ½λ©ν μ€νΈ] κ²°νΌμ (0) | 2022.02.14 |
[JAVA/μ½λ©ν μ€νΈ] μ¨λ¦ μ μ(Greedy Algorithm) (0) | 2022.02.12 |
[JAVA/μ½λ©ν μ€νΈ] μ‘°ν©μ κ²½μ°μ(λ©λͺ¨μ΄μ μ΄μ ) (0) | 2022.02.10 |
Comments