Coding Test/백준
[C++/Python] 백준 11055 - 가장 큰 증가하는 부분 수열
Junchoi
2020. 8. 4. 14:00
문제 링크
https://www.acmicpc.net/problem/11055
11055번: 가장 큰 증가 부분 수열
수열 A가 주어졌을 때, 그 수열의 증가 부분 수열 중에서 합이 가장 큰 것을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {1, 100, 2, 50, 60, 3, 5, 6, 7, 8} 인 경우에 합이 가장 큰 증가 부분 수�
www.acmicpc.net
현재의 값이 이전의 값보다 크면 증가 부분 수열이므로 현재 위치 까지의 모든 증가 부분 수열을 배열 dp에 저장해놓고 배열 중 최대값을 구하면 됩니다.
구현 코드
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 <iostream> | |
#include <algorithm> | |
#define endl '\n' | |
#define fastio cin.sync_with_stdio(false); cin.tie(nullptr) | |
using namespace std; | |
const int MAX = 1001; | |
int num[MAX]; | |
int dp[MAX]; | |
int main(){ | |
fastio; | |
int n; | |
int max_num = 0; | |
cin >> n; | |
for(int i=1;i<=n;i++) | |
cin >> num[i]; | |
for(int i=1;i<=n;i++){ | |
dp[i] = num[i]; | |
for(int j=1;j<i;j++){ | |
if(num[i] > num[j]){ | |
dp[i] = max(dp[i], dp[j] + num[i]); | |
} | |
} | |
max_num = max(max_num,dp[i]); | |
} | |
cout << max_num << endl; | |
return 0; | |
} |
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
n = int(input()) | |
num = list(map(int, input().split())) | |
dp = [0 for i in range(n)] | |
for i in range(n): | |
dp[i] = num[i] | |
for j in range(i): | |
if num[i] > num[j]: | |
dp[i] = max(dp[i], dp[j] + num[i]) | |
print(max(dp)) |