728x90
알고리즘 분류: DP, LIS
문제 링크: www.acmicpc.net/problem/11054
11054번: 가장 긴 바이토닉 부분 수열
첫째 줄에 수열 A의 크기 N이 주어지고, 둘째 줄에는 수열 A를 이루고 있는 Ai가 주어진다. (1 ≤ N ≤ 1,000, 1 ≤ Ai ≤ 1,000)
www.acmicpc.net
- LIS를 활용하면 간단하게 구현할 수 있는 DP문제이다.
- 왼쪽부터 LIS, 오른쪽부터 r_LIS배열에 저장한 후 최대 길이(두 LIS의 합-1)를 구하면 된다.
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <climits>
#include <cstring>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define ll long long
#define INF 1e9
using namespace std;
//https://www.acmicpc.net/problem/11054 가장 긴 바이토닉 부분 수열
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
int ar[1001] = {};
int LIS[1001] = {};
int r_LIS[1001] = {};
int ans = 0;
cin >> n;
for(int i=1; i<=n; i++) cin >> ar[i];
for(int i=1; i<=n; i++) {
LIS[i] = 1;
r_LIS[n-i+1] = 1;
for(int j=1; j<i; j++) {
if(ar[j] < ar[i]) LIS[i] = max(LIS[i], LIS[j] + 1);
if(ar[n-j+1] < ar[n-i+1]) r_LIS[n-i+1] = max(r_LIS[n-i+1], r_LIS[n-j+1] + 1);
}
}
for(int i=1; i<=n; i++) ans = max(ans, LIS[i] + r_LIS[i] - 1);
cout << ans;
return 0;
}
728x90
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준 / BOJ] 1753 최단경로 (0) | 2021.03.17 |
---|---|
[백준 / BOJ] 2667 단지번호붙이기 (0) | 2021.03.16 |
[백준 / BOJ] 1992 쿼드트리 (0) | 2021.03.16 |
[백준 / BOJ] 11866 요세푸스 문제 0 (0) | 2021.03.16 |
[백준 / BOJ] 1541 잃어버린 괄호 (0) | 2021.03.16 |