洛谷P1020 導彈攔截 題解 LIS擴展題 Dilworth定理

題目連接:https://www.luogu.com.cn/problem/P1020html

題目大意:
給你一串數,求:c++

  1. 這串數的最長不上升子序列的長度;
  2. 最少劃分紅多少個子序列是的這些子序列都是不上升子序列。

第一個問題比較簡單,就是用二分的方法 O(log n) 能夠解決這個問題。spa

第二個問題,能夠用 Dilworth定理 證實:code

在一個序列中,最長不上升子序列的最少劃分數就等於其最長上升子序列的長度htm

Dilworth定理參考自:http://www.javashuo.com/article/p-zsdtwnnm-hc.htmlblog

實現代碼以下:get

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, cnt, a[maxn], h[maxn], ans;
int main() {
    while (~scanf("%d", &a[n])) n ++;
    for (int i = 0; i < n; i ++) {
        int id = upper_bound(h, h+cnt, a[i], greater<int>()) - h;
        if (id == cnt) h[cnt++] = a[i];
        else h[id] = a[i];
    }
    ans = cnt;
    cnt = 0;
    for (int i = 0; i < n; i ++) {
        int id = lower_bound(h, h+cnt, a[i]) - h;
        if (id == cnt) h[cnt++] = a[i];
        else h[id] = a[i];
    }
    printf("%d\n%d\n", ans, cnt);
    return 0;
}
相關文章
相關標籤/搜索