https://www.acmicpc.net/problem/1818
생각 흐름
- 핵심은 증가하는 가장 긴 배열을 그대로 두고 나머지를 옮겨주면 된다.
- 예를 들어 1 3 4 7 6 2 일 때 1 3 4 6 이 증가하는 가장 긴 배열이다.
- 이로 인해 정답은 N - 증가하는 가장 긴 배열의 크기를 넣으면 된다.
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int N;
vector<int> input;
int dp[250000];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int answer = 0;
cin>>N;
for(int i = 0; i<N; i++){
int tmp;
cin>>tmp;
input.push_back(tmp);
}
dp[0]=-987654321;
for(int i = 1; i<=N; i++){
dp[i]=987654321;
}
for(int i = 0; i<N; i++){
auto a = lower_bound(dp, dp+(N+1), input[i]) - dp;
dp[a] = input[i];
answer = max(answer, (int)a);
}
cout<<(N-answer)<<endl;
return 0;
}
'코딩테스트 > C++' 카테고리의 다른 글
[ 백준 BOJ ] 28069번 - 김밥천국의 계단 (C++) (0) | 2025.06.25 |
---|---|
[ 백준 BOJ ] 2151번 - 거울 설치 (C++) (2) | 2025.06.23 |
[ 백준 BOJ ] 16472번 - 고냥이 (C++) (0) | 2025.06.21 |
[ 백준 BOJ ] 2091번 - 동전 (C++) (0) | 2025.06.19 |
[ 백준 BOJ ] 21276번 - 계보 복원가 호석 (C++) (0) | 2025.06.16 |