lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
思路:每次加入一辆车时,先判断原来轨道的最后一辆车的序号是否有大于要加入的,如果有则将它加入序号最小且大于该车序号的轨道,否则额外添加一个轨道。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
set<int> s;
cin>>n;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(s.upper_bound(x)!=s.end())
s.erase(s.upper_bound(x));
s.insert(x);
}
cout<<s.size();
return 0;
}