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
- 투 포인터
- 수학
- 다익스트라
- 다이나믹 프로그래밍
- XR Interaction Toolkit
- ue5
- Unreal Engine 5
- c++
- 브루트포스
- 그래프
- 시뮬레이션
- 구현
- 정렬
- 우선순위 큐
- 백트래킹
- 누적 합
- 재귀
- 문자열
- 자료구조
- 스택
- 그리디 알고리즘
- 유니온 파인드
- 백준
- 유니티
- VR
- 알고리즘
- Team Fortress 2
- BFS
- DFS
- 트리
Archives
- Today
- Total
1일1알
백준 2296번 건물짓기 C++ 본문
1. 건물들을 x좌표 오름차순으로 정렬한다.
2. 좌표가 대각선 위로 올라가는 건물들과 대각선 아래로 내려가는 건물들의 이익을 따로 구하고 이익이 최대가 되는 경우를 구한다.
순열 문제와 비슷하게 푼 것 같다.
#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>
#include <iomanip>
using namespace std;
using ll = long long;
struct Building {
int x;
int y;
int c;
};
bool compare(const Building& lhs, const Building& rhs) {
return lhs.x < rhs.x;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, x, y, c;
cin >> n;
vector<Building> v;
vector<int> dp13(n, 0);
vector<int> dp24(n, 0);
for (int i = 0; i < n; i++) {
cin >> x >> y >> c;
v.push_back({ x,y,c });
}
sort(v.begin(), v.end(), compare);
for (int i = 0; i < n; i++) {
dp13[i] = dp24[i] = v[i].c;
}
int ans = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (v[j].y < v[i].y) {
dp13[i] = max(dp13[i], dp13[j] + v[i].c);
}
if (v[j].y > v[i].y) {
dp24[i] = max(dp24[i], dp24[j] + v[i].c);
}
}
ans = max(ans, dp13[i]);
ans = max(ans, dp24[i]);
}
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 1028번 다이아몬드 광산 C++ (0) | 2022.02.17 |
---|---|
백준 1099번 알 수 없는 문장 C++ (0) | 2022.02.16 |
백준 2418번 단어 격자 C++ (0) | 2022.02.14 |
백준 2705번 팰린드롬 파티션 C++ (0) | 2022.02.13 |
백준 3258번 컴포트 C++ (0) | 2022.02.12 |