这道题思路很巧妙,记录一下
-
题目链接:传送门
-
思路: 这道题既然要求火车编号递减从出口离开,所以在进入入口的时候,就应该该火车跟随在比最小编号它编号大的火车后面, 若该编号比任何一个已经安排好的最小编号都大,就新开一个轨道。
在寻找该火车应该跟在哪辆火车后面时,应该选择编号最接近它 且 比它大的火车。可以用lower_bound 二分查找一下 -
代码:
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 1000010;
typedef pair<int,int> PII;
int n;
int a[N];
set<int> st;
int main() {
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
st.insert(a[1]);
for(int i=2;i<=n;i++){
set<int>::iterator it = st.lower_bound(a[i]);
if(it!=st.end()){
st.erase(it);
st.insert(a[i]);
} else{
st.insert(a[i]);
}
}
cout<<st.size();
return 0;
}