Post

[PCCP 모의고사 2회] 신입사원 교육 (C++)

https://school.programmers.co.kr/learn/courses/15009/lessons/121688

풀이

신입 사원들의 능력치를 오름차순으로 표현한 우선순위 큐를 이용했다. 가장 앞에 있는 2명의 능력치를 더하고 다시 우선순위 큐에 넣어주는 코드를 number만큼 반복한 후, 우선순위 큐에 있는 모든 능력치의 합을 반환해 풀 수 있었다.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <vector>
#include <queue>

using namespace std;

int solution(vector<int> ability, int number) {
    priority_queue<int, vector<int>, greater<int>> pq(ability.begin(), ability.end()); 
    for(int i = 0; i < number; i++){
        int sum = 0;
        for(int j = 0; j < 2; j++)
            sum += pq.top(),    pq.pop();
        pq.push(sum),   pq.push(sum);
    }
    
    int answer = 0;
    while(!pq.empty()){
        answer += pq.top();
        pq.pop();
    }
    return answer;
}
This post is licensed under CC BY 4.0 by the author.