Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- VR
- 재귀
- c++
- 그래프
- Unreal Engine 5
- 시뮬레이션
- 스택
- 수학
- 누적 합
- XR Interaction Toolkit
- 다이나믹 프로그래밍
- 구현
- 자료구조
- Team Fortress 2
- 유니티
- 백준
- 다익스트라
- 유니온 파인드
- ue5
- BFS
- 백트래킹
- 브루트포스
- 문자열
- 트리
- 우선순위 큐
- 그리디 알고리즘
- 정렬
- 알고리즘
- 투 포인터
- DFS
Archives
- Today
- Total
1일1알
백준 1680번 쓰레기 수거 C++ 본문

가까운 쓰레기장부터 방문하면서 실었는데 용량을 넘기거나 실었는데 용량이 가득 찼을 때 처음으로 되돌아가서 버리고 다시 온다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int w, n;
cin >> w >> n;
vector<pair<int, int>> trash(n);
for (int i = 0; i < n; i++) {
int x_i, w_i;
cin >> x_i >> w_i;
trash[i] = make_pair(x_i, w_i);
}
int sum = 0;
int curr_lot = 0;
int curr_weight = 0;
int idx = 0;
while (idx < n) {
if (curr_weight + trash[idx].second == w) {
sum += trash[idx].first;
curr_weight = 0;
idx++;
continue;
}
else if (curr_weight + trash[idx].second < w) {
curr_weight += trash[idx].second;
idx++;
continue;
}
else {
sum += trash[idx].first;
curr_weight = 0;
}
}
if (curr_weight > 0) {
sum += trash[n - 1].first;
}
cout << sum * 2 << "\n";
}
};
'알고리즘' 카테고리의 다른 글
백준 17259번 선물이 넘쳐흘러 C++ (0) | 2022.02.02 |
---|---|
백준 2818번 숙제하기 싫을 때 C++ (0) | 2022.02.01 |
백준 2016번 미팅 주선하기 C++ (0) | 2022.02.01 |
백준 3987번 보이저 1호 C++ (0) | 2022.02.01 |
백준 10431번 줄세우기 C++ (0) | 2022.01.31 |