acwing 1010 拦截导弹 (最长上升子序列)

题面

在这里插入图片描述

题解

  1. 第一问就是最长上升子序列的模型,注意这里求的是最长的不上升子序列
  1. 第二问: 我们要想序列个数尽可能的少,那么每个序列所放的元素就要尽可能的多,因为要求序列中放的元素不递增,那么对于新加入的元素,我们肯定要放在与这个新元素相差最小且比它大的元素所在队列的后边

在这里插入图片描述

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <set>

using namespace std;
const int N = 1010;

int x;
int n = 1;
int h[N];
int f[N];

int main() {
    
    

    while (cin >> x) h[n++] = x;
    n--;
    int res = 0;
    for (int i = 1; i <= n; i++) {
    
    
        f[i] = 1;
        for (int j = 1; j < i; j++) {
    
    
            if (h[i] <= h[j]) {
    
    
                f[i] = max(f[i], f[j] + 1);
            }
        }
        res = max(res, f[i]);
    }
    cout << res << endl;

    set<int> st;
    for (int i = 1; i <= n; i++) {
    
    
        //队列为空或者是所有队列都小于这个数
        if (st.empty() || st.lower_bound(h[i]) == st.end()) {
    
    
            st.insert(h[i]);
        }else{
    
    //否则就放到离他最近且比它大的数这个队列
            st.erase(st.lower_bound(h[i]));
            st.insert(h[i]);
        }
    }
    cout<<st.size()<<endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44791484/article/details/115196410