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
- 시뮬레이션
- 스택
- 다익스트라
- 문자열
- 유니온 파인드
- 트리
- 유니티
- 자료구조
- 투 포인터
- 누적 합
- 브루트포스
- ue5
- XR Interaction Toolkit
- 백트래킹
- 다이나믹 프로그래밍
- 구현
- 정렬
- 백준
- 수학
- Unreal Engine 5
- 그래프
- 재귀
- c++
- DFS
- 그리디 알고리즘
- BFS
- 우선순위 큐
- 알고리즘
- VR
- Team Fortress 2
Archives
- Today
- Total
1일1알
백준 10835번 카드게임 C++ 본문
오른쪽 카드가 왼쪽 카드보다 작은 경우와, 큰 경우 두 가지로 나누어서 문제를 해결하였다.
오른쪽 카드가 왼쪽 카드보다 작은 경우는
1. 오른쪽 카드의 점수를 얻고 오른쪽 카드를 빼는 경우
2. 왼쪽 카드를 빼는 경우
3. 양쪽 카드를 빼는 경우
이 세 가지 중 큰 값을 구했고
반대의 경우에는
1. 왼쪽 카드를 빼는 경우
2. 양쪽 카드를 빼는 경우
이 두 가지 중 큰 값을 구했다.
그리고 같은 지점을 계속 탐색할 수 있기 때문에 시간 초과나 스택 오버플로우가 발생할 수 있다.
그렇기 때문에 메모이제이션 기법을 통해 dp배열에 방문한 곳을 저장해서 바로 빠져나올 수 있게 작성하였다.
#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;
int c_Left[2001];
int c_Right[2001];
vector<vector<int>> dp(2001, vector<int>(2001, -1));
int solve(int left, int right) {
if (left > n || right > n) {
return 0;
}
if (dp[left][right] != -1) return dp[left][right];
if (c_Left[left] > c_Right[right]) {
dp[left][right] = max(solve(left, right + 1) + c_Right[right], max(solve(left + 1, right + 1), solve(left + 1, right)));
}
else {
dp[left][right] = max(solve(left + 1, right + 1), solve(left + 1, right));
}
return dp[left][right];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c_Left[i];
}
for (int i = 1; i <= n; i++) {
cin >> c_Right[i];
}
int ans = solve(1, 1);
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 1790번 수 이어 쓰기 2 C++ (0) | 2021.12.24 |
---|---|
백준 16987번 계란으로 계란치기 C++ (0) | 2021.12.23 |
백준 9009번 피보나치 C++ (0) | 2021.12.21 |
백준 15661번 링크와 스타트 C++ (0) | 2021.12.20 |
백준 3079번 입국심사 C++ (0) | 2021.12.19 |