일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 조합론
- DP
- 회고록
- 다익스트라
- pytorch
- 세그먼트 트리
- 플로이드 와샬
- 가끔은_말로
- c++
- lazy propagation
- dfs
- 자바스크립트
- back propagation
- 백트래킹
- 우선 순위 큐
- 크루스칼
- 분할 정복
- 문자열
- 2023
- dropout
- object detection
- BFS
- 알고리즘
- 이분 탐색
- Overfitting
- 미래는_현재와_과거로
- tensorflow
- 가끔은 말로
- NEXT
- 너비 우선 탐색
Archives
- Today
- Total
Doby's Lab
백준 11256번: 사탕 (C++) 본문
https://www.acmicpc.net/problem/11256
Solved By: Greedy Algorithm
상자를 최대한 적게 쓸려면 사이즈가 큰 순서대로 상자를 쓰면 된다. => 그리디 알고리즘인 이유
상자의 사이즈 크기 순서대로 배열을 정렬하고,
사탕의 개수에서 상자의 크기를 하나씩 빼가면서 다 뺐을 경우, 몇 개까지 상자를 사용했는지 저장하여 출력했다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(int a, int b){return a > b;}
int main(){
int T; cin >> T;
vector<int> res;
while(T--){
int n, m; cin >> n >> m;
vector<int> v;
for(int i = 0; i < m; i++){
int r, c; cin >> r >> c;
v.push_back(r * c);
}
sort(v.begin(), v.end(), cmp);
int cnt = 0;
for(int i = 0; i < v.size(); i++){
n -= v[i];
if(n <= 0){
res.push_back(i + 1);
break;
}
}
}
for(auto v : res) cout << v << '\n';
}
728x90
'PS > BOJ' 카테고리의 다른 글
백준 4233번: 가짜소수 (C++) (0) | 2022.06.25 |
---|---|
백준 14565번: 역원(Inverse) 구하기 (C++) (0) | 2022.06.21 |
백준 15720번: 카우버거 (C++) (0) | 2022.06.19 |
백준 14955번: How Many to Be Happy? (C++) (0) | 2022.06.18 |
백준 14248번: 점프 점프 (C++) (0) | 2022.06.16 |