🌷🌼λͺ¨μ—¬λ΄μš” 개발의숲🌷🌼

[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));
    }

}
Comments