View

유형: 그리디

문제: https://www.acmicpc.net/problem/11256

 

11256번: 사탕

당신은 사탕 공장의 주인이다. 날마다, 당신은 J개의 사탕을 가게에 보내기 위해 상자에 포장해야 한다. 당신은 크기가 다른 상자 N개를 가지고 있다. 당신은 편리를 위해 상자를 최소한으로 쓰

www.acmicpc.net

 

풀이

상자는 최대 가로*세로의 개수만큼 사탕을 담을 수 있다.

가로*세로 값을 벡터에 저장한 뒤, 내림차순으로 정렬한다.

그리고 벡터 원소를 하나씩 돌면서(가장 큰 값부터 보게 된다) 전체 사탕 개수 -= 원소값 으로 갱신한다.

이때 사탕 개수가 0 이하가 된다면 사탕을 전부 담게된다.

 

#include "iostream"
#include "vector"
#include "algorithm"

using namespace std;

int solution(vector<int> arr, int total){
    int cnt = 0;

    sort(arr.begin(), arr.end(), greater<>());

    for(auto now: arr){
        total -= now;
        cnt ++;
        if(total <= 0) break;
    }

    return cnt;
}

int main(){
    ios::sync_with_stdio(0);
    int test; cin >> test;

    while(test--){
        int total, box; cin >> total >> box;
        vector<int> arr;

        for(int i = 0; i < box; i++){
            int x, y; cin >> x >> y;
            arr.push_back(x*y);
        }

        cout << solution(arr, total) << endl;
    }

    return 0;
}

 

Share Link
reply
«   2025/02   »
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