C++ PAT乙 1045. 快速排序(25)
这题用vector会方便一些。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int n;
cin >> n;
long long a[n],max = -1;;
vector<long long> v;
for(int i = 0;i < n;i++){
cin >> a[i];
if(a[i] >= max) v.push_back(a[i]),max = a[i];
else{
for(int j = 0;j < v.size();j++)
if(v[j] > a[i]) v.erase(v.begin()+j),j--;
}
}
sort(v.begin(),v.end());
cout<<v.size()<<endl;
for(int i = 0;i < v.size();i++){
cout << v[i];
if(i != v.size()-1) cout<<" ";
}
cout << endl;
return 0;
}