BOJ : https://www.acmicpc.net/problem/2631

github : https://github.com/junho0956/Algorithm/blob/master/2631/2631/%EC%86%8C%EC%8A%A4.cpp

 

어떻게 푸는건지 감을 못잡았는데

문제 예시에서 움직이는 4, 7, 1, 2 를 제외한 3, 5, 6은 이미 정렬이 되어있는 상태였고

3, 5, 6 자체가 증가하는 부분수열이니

문제에서 가장 긴 증가하는 부분수열을 찾는다는 생각으로 lis를 구현하였다

 

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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
int N;
vector<int> dp;
int arr[201];
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
 
    cin >> N;
    for (int i = 0; i < N; i++cin >> arr[i];
 
    dp.push_back(arr[0]);
    int lis = 0;
 
    for (int i = 1; i < N; i++) {
        if (dp[lis] < arr[i]) {
            dp.push_back(arr[i]);
            lis++;
        }
        else {
            int lo = lower_bound(dp.begin(), dp.end(), arr[i]) - dp.begin();
            dp[lo] = arr[i];
        }
    }
 
    cout << N - (lis + 1);
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 11568번 민균이의 계략  (0) 2020.02.11
BOJ 7570번 줄 세우기  (0) 2020.02.10
BOJ 2605번 줄 세우기  (0) 2020.02.10
BOJ 1681번 줄 세우기  (0) 2020.02.10
BOJ 4781번 사탕가게  (0) 2020.02.10

+ Recent posts