set容器的妙用

LXK有一個序列,從N~1,但是他不小心把序列打亂了,現在他想找你把這串序列復原。
他討厭用傳統的方式排序。所以他用他自己的方式進行復原。
他有K個先進先出的隊列
對於某個數字,你可以選擇將其放入任意隊列之中(不能不放)。
每個隊列中隊首的數字可以在任意時間出隊列。
利用這些隊列,聰明的LXK就可以將序列復原回降序。
他想知道這些操作最少需要準備多少個隊列?

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<set>
using namespace std;
const int maxn=1e5+5;
int n;
int a[maxn];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    set<int> ans;
    set<int>::iterator it;
    for(int i=1;i<=n;i++){
       it=upper_bound(ans.begin(),ans.end(),a[i]);
       if(it!=ans.end()){
          ans.erase(it);
          ans.insert(a[i]);
       }
       else{
        ans.insert(a[i]);
       }
    }
    cout<<ans.size()<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章