最長(zhǎng)非減字串2 (二分查找) 銀牌
2022-10-19 22:16 作者:信奧賽USACO鄭老師 | 我要投稿
//最長(zhǎng)遞增(非減少)子序列
//給定長(zhǎng)度尾部最小值遞推動(dòng)態(tài)規(guī)劃,O(nlogn)
#include<bits/stdc++.h>
using namespace std;
int main(){
? ? int n;
? ? cin>>n;
? ? vector<int> a;
? ? for(int i=0;i<n;i++){
? ? ? ? int x;
? ? ? ? cin>>x;
? ? ? ? auto ip=upper_bound(a.begin(),a.end(),x);
? ? ? ? if(ip==a.end()){//larger than anything ending
? ? ? ? ? ? a.push_back(x);
? ? ? ? }else{
? ? ? ? ? ? if(*ip>x){
? ? ? ? ? ? *ip=x;//replace larger minimal value
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? cout<<a.size()<<endl;
}
標(biāo)簽: