본문 바로가기

백준

2565 전깃줄

처음에는 전깃줄 두 개 따로 저장한 다음 구했는데 틀려서 고민 엄청 하다가 아래 그림 보니 a를 기준으로 b의 증가하는 수열을 구하면 될 것 같았다. 

 

 

그래서 pair로 저장한 다음 xx를 기준으로 정렬한 다음 LIS로 구했다. 

 

이 문제가 LIS라서 바로 LIS로 구했긴 한데 증가하는 수열을 고르면 왜 정답이 되는지는 증명하지 못하겠다.. 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()
 
using namespace std;
using i64 = long long;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

bool sortbyfirst(const pair<int,int> &a, const pair<int,int> &b) 
{ 
    return (a.first < b.first); 
}

int main() {
    int n;
    scanf("%d", &n);
    
    vector<ii> v(n);
    vector<int> dp(n);
    for (int i = 0; i < n; i++)
        scanf("%d %d", &v[i].xx, &v[i].yy);
    
    sort(all(v), sortbyfirst);
    
    int max = 0;
    for (int i = 0; i < n; i++)
    {
        if (dp[i] == 0)
            dp[i] = 1;
        for (int j = 0; j < i; j++)
        {
            if (v[i].yy > v[j].yy)
            {
                if (dp[i] < dp[j] + 1)
                    dp[i] = dp[j] + 1;
            }
        }
        
        if (max < dp[i])
            max = dp[i];
    }
    
    cout << n - max << endl;
    
    return 0;
}

'백준' 카테고리의 다른 글

2022 사다리  (0) 2020.08.03
14002 가장 긴 증가하는 부분 수열 4  (0) 2020.08.03
11053 가장 긴 증가하는 부분 수열  (0) 2020.08.02
1149 RGB거리  (0) 2020.07.31
1463 1로 만들기  (0) 2020.07.31