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 |
Tags
- XR Interaction Toolkit
- Team Fortress 2
- c++
- 재귀
- Unreal Engine 5
- DFS
- 투 포인터
- 누적 합
- 자료구조
- 백준
- 정렬
- 구현
- 스택
- 수학
- 그래프
- ue5
- 브루트포스
- 유니티
- 우선순위 큐
- 다이나믹 프로그래밍
- 문자열
- 트리
- 시뮬레이션
- 그리디 알고리즘
- BFS
- 백트래킹
- 알고리즘
- VR
- 다익스트라
- 유니온 파인드
Archives
- Today
- Total
1일1알
백준 8980번 택배 C++ 본문
1. 도착지점을 기준으로 오름차순 정렬을 한다. 도착지점이 같을 경우에는 시작지점을 기준으로 오름차순 정렬을 한다.
(시작지점을 기준으로 정렬을 하면 독점이 일어날 수 있다.)
2. 각 지점마다 트럭의 용량을 저장하는 벡터를 만들고, 시작지점에서 도착지점까지 중 가장 트럭에 짐이 많이 실린 용량
을 찾는다.
3. 트럭의 짐의 용량 + 내 용량이 c보다 크면 짐을 실을 수 있는 만큼만 자른다.
4. 시작지점부터 도착지점까지 내 짐의 무게가 트럭에 더해진다.
5. 2~4를 m만큼 반복한다.
#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;
typedef long long ll;
int n, c, m;
struct box {
box(int start, int end, int capacity) :start(start), end(end), capacity(capacity) {}
int start;
int end;
int capacity;
};
bool compare(const box& lhs, const box& rhs) {
if (lhs.end == rhs.end) {
return lhs.start < rhs.start;
}
return lhs.end < rhs.end;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> c >> m;
int ans = 0;
vector<box> boxes;
vector<int> truck(n + 1, 0);
for (int i = 0; i < m; i++) {
int start, end, capacity;
cin >> start >> end >> capacity;
boxes.push_back(box(start, end, capacity));
}
sort(boxes.begin(), boxes.end(), compare);
for (int i = 0; i < m; i++) {
int max_capacity = 0;
//시작지점에서 도착지점까지 중 가장 트럭에 짐이 많이 실린 용량을 찾는다.
for (int j = boxes[i].start; j < boxes[i].end; j++) {
max_capacity = max(max_capacity, truck[j]);
}
// 트럭의 짐의 용량 + 내 용량이 c보다 크면 짐을 실을 수 있는 만큼만 자른다.
int myCapacity = boxes[i].capacity;
if (myCapacity + max_capacity > c) {
myCapacity = c - max_capacity;
}
ans += myCapacity;
//시작지점부터 도착지점까지 내 짐의 무게가 트럭에 더해진다.
for (int j = boxes[i].start; j < boxes[i].end; j++) {
truck[j] += myCapacity;
}
}
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 1491번 나선 C++ (0) | 2022.01.30 |
---|---|
백준 2777번 숫자 놀이 C++ (0) | 2022.01.29 |
백준 1083번 소트 C++ (0) | 2022.01.27 |
백준 1092번 배 C++ (0) | 2022.01.26 |
백준 1474번 밑 줄 C++ (0) | 2022.01.25 |