这道题,第一次写用了vector<stack<int>>,去存储每个铁轨的火车情况,对于每个火车看看能不能找到一个末尾比他大的,如果比他大,就跟在他后面,自信提交,毫无疑问wa了,然后发现,我们不用去知道每个铁轨的具体情况,只需要存储末尾的那一个,也就是最小的,并且可以证明,一定是递增的,如:如果下一个铁轨的末尾比上一个铁轨的末尾小,那我们一定可以将下一个铁轨的末尾也放在上一个铁轨的末尾的后面,所以下一个铁轨的末尾一定比上一个铁轨的末尾大,所以可以使用二分查找,用log的复杂度去找到第一个大于当前火车编号的轨道,然后更新当前轨道的最小值,并且这个新加入的火车编号一定比下一个轨道的末尾小,比上一个轨道的末尾大。
// Problem: 智乃的“K”叉树
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/103957/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int long long
typedef long long ll;
const int N = 1e5 + 10;
const int mod = 998244353;
void solve() {
int n;
cin>>n;
vector<int> a(n+1);
for(int i = 1 ; i <= n ; i++){
cin>>a[i];
}
vector<int> ans;
for(int i = 1 ; i <= n ; i++){
if(ans.size() == 0){
ans.push_back(a[i]);
continue;
}
int idx = lower_bound(ans.begin(),ans.end(),a[i]) - ans.begin();
if(idx >= ans.size()){
ans.push_back(a[i]);
}else{
ans[idx] = a[i];
}
}
cout<<ans.size()<<endl;
/*
1 2 4 8
3 5
6 9
7
*/
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
return 0;
}