본문 바로가기
알고리즘/DP

색종이 더블릿 (업시퀀스)

by tryotto 2019. 2. 21.
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
42
43
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <vector>
 
using namespace std;
vector<pair<intint> > arr(10005);
int dp[10005= { 0 };
 
bool compare(const pair<intint>& a, const pair<intint>& b) {
    if (a.first == b.first)
        return a.second < b.second;
    return a.first < b.first;
}
 
int main() {
    int n;
    scanf("%d"&n);
 
    for (int i = 1; i <= n; i++) {
        scanf("%d %d"&arr[i].first, &arr[i].second);
        if (arr[i].first > arr[i].second)
            swap(arr[i].first, arr[i].second);
    }
    sort(&arr[1], &arr[n + 1], compare);
    
    dp[1= 1;
    for (int i = 2; i <= n; i++) {
        int maxNum = 0;
        for (int j = 1; j < i; j++) {
            if (arr[j].second <= arr[i].second) {
                maxNum = max(maxNum, dp[j]);
            }
        }
        dp[i] = maxNum + 1;
    }
 
    int maxNum = 0;
    for (int i = 1; i <= 10000; i++) {
        maxNum = max(maxNum, dp[i]);            
    }
    printf("%d", maxNum);
}
cs


'알고리즘 > DP' 카테고리의 다른 글

2193 이친수  (0) 2019.07.05
(미해결) buy lower 더블릿  (0) 2019.02.23
블록쌓기 더블릿 (업시퀀스)  (0) 2019.02.21
줄 세우기 더블릿  (0) 2019.02.21
up sequence 더블릿 (LIS)  (0) 2019.02.20