์‹ค๋ฒ„1 : ๋™์ ๊ณ„ํš๋ฒ• ๋ฌธ์ œ์ด๋‹ค.

max_element ์ตœ๋Œ€ํ•œ ์“ฐ์ง€ ๋ง์ž. ๊ทธ๋ฆฌ๊ณ  sizeof๋Š” ๋ฐ”์ดํŠธ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ค€๋‹ค. ์ œ๋ฐœ..ใ… ใ… 

Code1

dp[n] = n๋ฒˆ์งธ ์ „๊นƒ์ค„์„ ํฌํ•จํ•œ ์ƒํƒœ๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ „๊นƒ์ค„ ๊ฐฏ์ˆ˜

dp[n] = max(dp[1~n-1] + 1)

 
#include <iostream>
#include <algorithm>
using namespace std;
int N;
int M = 0;
int map[501] = {0,};
int dp[501] = {0,};
int ans = -1;
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> N;
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        map[a] = b;
        dp[a] = 1;
        
        M = max(M, a);
    }
    
    for (int i = 1; i <= M; i++) {
        for (int j = 1; j < i; j++) {
            if (map[i] < map[j] || dp[i] == 0) {
                continue;
            } else {
                if (dp[i] < dp[j]+1) {
                    dp[i] = dp[j]+1;
                }
            }
        }
    }
    for (int i = 0; i <= M; i++) {
        ans = max(ans, dp[i]);
    }
    cout << N-ans << '\n';
    return 0;
}
 
 

Code2

8 2 9 1 4 6 7 10
2 4 6 7 10

๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด(LIS)๋กœ ํ’€ ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ์ „์— ๊ณ ๋“ฑํ•™๊ต ๋•Œ ํ•จ์ˆ˜ ๊ฐœ์ˆ˜ ์ฐพ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์น˜์—ญ ๋ถ€๋ถ„์—์„œ ์ •์˜์—ญ ์›์†Œ๋งŒํผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฝ‘์•„์ฃผ๊ฒŒ๋˜๋ฉด ์ž์—ฐ์Šค๋ ˆ ์›ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค. ์•ฝ๊ฐ„ ๊ทธ ๋Š๋‚Œ๊ณผ ๋น„์Šทํ•œ๋ฐ, ํ•ด๋‹น ๋ชจ์–‘์—์„œ ๋‹ต์ธ ์ƒํ™ฉ์€ 1 4 6 7 10 ๋˜๋Š” 2 4 6 7 10 ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์ด๋‹ค. ํŠน์ง•์„ ์‚ดํŽด๋ณด๋ฉด ๊ฐ’์ด ์ฆ๊ฐ€ํ•˜๊ณ  ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ A ์ „๋ด‡๋Œ€์—์„œ ๊ฐ€๋ฆฌํ‚ค๋Š” B ๊ฐ’๋“ค ์ค‘์—์„œ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜์—ด ์ค‘ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์„ ์ฐพ๋Š”๋‹ค๋ฉด, ๊ทธ๊ฒƒ์ด ์ตœ๋Œ€ ์ „๊นƒ์ค„์˜ ๊ฐœ์ˆ˜์ผ ๊ฒƒ์ด๋‹ค.

 
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int N;
int dp[101];
vector<pair<int, int>> input;
 
bool compare(pair<int, int> a, pair<int, int> b){
    return a.first < b.first;
}
int main(){
    cin >> N;
    input.push_back(make_pair(0, 0));
    for (int i = 1; i <= N; i++) dp[i] = 1;
 
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        input.push_back(make_pair(a, b));
    }
    sort(input.begin(), input.end(), compare);
    
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j < i; j++) {
            if (input[i].second > input[j].second) {
                dp[i] = max(dp[i], dp[j]+1);
            }
        }
    }
    int ans = -1;
    for (int i = 1; i <= N; i++) {
        ans = max(ans, dp[i]);
    }
    cout << N-ans << '\n';
    
    return 0;
}
 

Reference