题解 | 最长不下降子序列

最长不下降子序列

https://www.nowcoder.com/practice/25da45d0d4fb4faba45094cbb0649062

#include <bits/stdc++.h>
#define il inline

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll N = 1e5 + 5, mod = 998244353, inf = 2e18;
const double esp=1e-4;

il void solve(){
    ll n;
    cin>>n;
    vector<ll>a(n+1),dp;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    //vector里面只有数组长度有意义,数据并没有意义
    dp.push_back(a[1]);
    for(int i=2;i<=n;i++){
        if(a[i]>dp.back()){
            dp.push_back(a[i]);
        }else{
            auto it=upper_bound(dp.begin(),dp.end(),a[i]);
            *it=a[i];
        }
    }
    cout<<dp.size();
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    
    int t = 1;
    
    //cin >> t;
    
    while (t--) {

        solve();

    }

    return 0;
}

全部评论

相关推荐

yubullym:双非目前 0 正式 offer,打算继续实习到 1 月准备春招了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务