문제) 백준 - 동적 계획법 (Dynamic Programming) - 가장 긴 감소하는 부분 수열
-> www.acmicpc.net/problem/11722
11722번: 가장 긴 감소하는 부분 수열
수열 A가 주어졌을 때, 가장 긴 감소하는 부분 수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 30, 10, 20, 20, 10} 인 경우에 가장 긴 감소하는 부분 수열은 A = {10, 30, 10, 20, 20, 10}
www.acmicpc.net
참고)
2021.03.02 - [Algorithm_note/틀린 문제] - [백준] 11053번 - 가장 긴 증가하는 부분 수열 (파이썬)
[백준] 11053번 - 가장 긴 증가하는 부분 수열 (파이썬)
문제) 백준 - 동적 계획법 (Dynamic programming) - 가장 긴 증가하는 부분 수열 -> www.acmicpc.net/problem/11053 11053번: 가장 긴 증가하는 부분 수열 수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을..
chocochip101.tistory.com
Python 소스 코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
input = sys.stdin.readline | |
n = int(input()) | |
num = list(map(int, input().split())) | |
length = [0] * n | |
for i in range(n): | |
for j in range(i): | |
if num[j] > num[i] and length[j] > length[i]: | |
length[i] = length[j] | |
length[i] += 1 | |
print(max(length)) |
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
#define endl "\n" | |
#define MAX 1001 | |
#define INF 987654321 | |
#define MOD 1001 | |
#define ll long long | |
#define int ll | |
using namespace std; | |
typedef pair<int, int> p; | |
int N; | |
int arr[MAX]; | |
int cache[MAX]; | |
int solve(int idx) { | |
int& ret = cache[idx]; | |
if (ret != -1) return ret; | |
ret = 1; | |
for (int i = idx + 1; i < N; ++i) { | |
if (arr[idx] > arr[i]) | |
ret = max(ret, solve(i) + 1); | |
} | |
return ret; | |
} | |
signed main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
memset(cache, -1, sizeof(cache)); | |
cin >> N; | |
for (int i = 0; i < N; ++i) | |
cin >> arr[i]; | |
int ans = 0; | |
for (int i = 0; i < N; ++i) | |
ans = max(ans, solve(i)); | |
cout << ans; | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 2252번 - 줄 세우기 (파이썬) 문제 및 풀이 (0) | 2021.03.10 |
---|---|
[백준] 11054번 - 가장 긴 바이토닉 부분 수열 (파이썬) 문제 및 풀이 (0) | 2021.03.09 |
[백준] 1890번 - 점프 (파이썬) 문제 및 풀이 (0) | 2021.03.09 |
[백준] 1520번 - 내리막 길 (C++, 파이썬) 문제 및 풀이 (0) | 2021.03.09 |
[백준] 1904번 - 01타일 (C++/파이썬) 문제 및 풀이 (0) | 2021.03.04 |
댓글