백준 2631번 줄세우기
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 31 32 33 34 35 36 37 38 39 40 41 | #include<iostream> #include<vector> #include<string> #include<queue> #include<cstring> #include <algorithm> #include<set> #include<map> using namespace std; int n; int cache[1001]; int S[1001]; int lis(int start) { int& ret = cache[start + 1]; if (ret != -1) return ret; ret = 1; for (int next = start + 1; next < n; next++) if (start == -1 || S[start] < S[next]) ret = max(ret, 1 + lis(next)); return ret; } int main(void) { ios_base::sync_with_stdio(0); cin.tie(0); memset(cache, -1, sizeof(cache)); cin >> n; for (int i = 0; i < n; i++) cin >> S[i]; cout << n-(lis(-1) - 1) << endl; return 0; } | cs |
문제 출처:https://www.acmicpc.net/problem/2631
가장 긴 증가수열을 찾아서 전체에서 수열크기를 빼주면 정답이된다.
'알고리즘 > BAEKJOON' 카테고리의 다른 글
백준 11054번 가장 긴 바이토닉 부분수열 (0) | 2019.01.25 |
---|---|
백준 2512번 예산 (0) | 2019.01.25 |
백준 11058번 크리보드 (0) | 2019.01.25 |
백준 1202번 보석 도둑 (0) | 2019.01.24 |
백준 1003번 피보나치 함수 (0) | 2019.01.23 |