문제접근
•
큐 자료구조 사용
•
순서가 중요하기 때문에 앞에서부터 하나씩 빼면서 상자의 최대 무게를 초과하지 않는 범위내로 하나씩 빼낸다
놓쳤던 부분
•
큐 자료구조이기 때문에 큐가 비어있는데 접근하려고 하는지를 확인해줘야함
•
생각해보니 입력 순서가 중요해서 입력 받자마자 처리하는게 더 좋을듯
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
int main() {
fastio;
int n, m;
cin >> n >> m;
int sum = 0;
int ans = 0;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
if (sum+x <= m) {
sum += x;
} else {
ans++;
sum = x;
}
}
if (sum > 0) ans++;
cout << ans;
}
C++
복사
코드
2020 KB
0 ms
#include <iostream>
#include <queue>
using namespace std;
int main(void) {
int n, m;
int input;
int answer = 0;
queue<int> boxes;
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> input;
boxes.push(input);
}
while (!boxes.empty()) {
int totalWeight = 0;
while (totalWeight < m) {
if (boxes.empty()) {
if (totalWeight != 0)
answer++;
break ;
}
if (totalWeight + boxes.front() > m) {
answer++;
break ;
}
if (totalWeight + boxes.front() == m)
answer++;
totalWeight += boxes.front();
boxes.pop();
}
}
cout << answer;
return (0);
}
C++
복사