[BOJ] 11256. 사탕 - (정렬,그리디) c++

ha·2022년 1월 26일
0

BOJ

목록 보기
9/28

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

C++ (그리디) 풀이

  1. 각 상자 가로 * 세로 임시배열에 저장 후 내림차순 정렬
  2. 배열 시작부터 누적합 비교
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int tc;


int main() {
    cin >> tc;
    while(tc--)
    {
        int j,N;
        cin>>j>>N;
        vector<int> tmp;
        for(int i=0;i<N;i++)
        {
            int a,b;
            cin>>a>>b;
            tmp.push_back(a*b);
        }
        sort(tmp.begin(),tmp.end(),greater<int>());
        int answer=0;
        int total=0;
        for(auto a : tmp)
        {
            total+=a;
            answer++;
            if(total>=j) {
                cout<<answer<<'\n';
                break;
            }
        }
    }
}

0개의 댓글