Acwing-----1010. 拦截导弹

算法

贪心算法:
    从前往后扫描每个数,对于每个数:
    1、如果现有子序列的结尾都小于当前数,则创建新序列;
    2、将当前数放到结尾大于等于它的最小的子序列后面。

代码

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1010;
int n;
int q[N], f[N], g[N];

int main() {
    while (cin >> q[n]) n++;
    int res = 0;
    
    for (int i = 0; i < n; ++i) {
        f[i] = 1;
        for (int j = 0; j < i; ++j) {
            if (q[j] >= q[i]) f[i] = max(f[i], f[j] + 1);
        }
        res = max(res, f[i]);
    }
    
    cout << res << endl;
    
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        int k = 0;
        while (k < cnt && g[k] < q[i]) k++;
        g[k] = q[i];
        if (k >= cnt) cnt++;
    }
    cout << cnt << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/clown9804/p/12600914.html