PAT 1045 最长不减子序列
最长不减子序列的变形,用map将颜色映射到其顺序,对其顺序求最长不减子序列即可。
需要注意的是,upper_bound函数的开闭性为左闭右开,自己写的二分一般是左闭右闭。
代码:
#include <iostream>
#include <map>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 10005;
int N,M,L,max_ = 0;
map<int,int> m;
int d[maxn];
int main(){
memset(d, 0, sizeof(d));
cin>>N>>M;
for(int i = 1;i <= M;i++){
int tmp;
cin>>tmp;
m[tmp] = i;
}
cin>>L;
for(int i = 1;i <= L;i++){
int tmp;
cin>>tmp;
if(m[tmp]==0) continue;
auto pos = upper_bound(d, d+max_+1, m[tmp]);
*pos = m[tmp];
if(pos-d > max_) max_ = pos-d;
}
cout<<max_<<endl;
}
27ms