Skip to content

Instantly share code, notes, and snippets.

@lishunan246
Created December 23, 2015 08:15
Show Gist options
  • Save lishunan246/7d39e261b09930076897 to your computer and use it in GitHub Desktop.
Save lishunan246/7d39e261b09930076897 to your computer and use it in GitHub Desktop.
1045. 快速排序(25)
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
int N;
cin >> N;
vector<pair<long long, bool>> v;
for (auto i = 0; i < N; ++i)
{
long long t;
cin >> t;
v.push_back({t,true});
}
decltype(v) result;
auto max = (*v.cbegin()).first;
for (auto i = v.begin(); i != v.end(); ++i)
{
if (i->first < max)
i->second = false;
else
max = i->first;
}
auto min = (*v.crbegin()).first;
for (auto i = v.rbegin(); i != v.rend(); ++i)
{
if (i->first > min)
i->second = false;
else
min = i->first;
}
copy_if(v.cbegin(), v.cend(), back_inserter(result), [](const pair<long long, bool> p)
{
return p.second;
});
cout << result.size() << endl;
if (result.size())
{
auto t = result.begin();
cout << t->first;
result.erase(t);
}
for_each(result.cbegin(), result.cend(), [](const pair<long long, bool> p)
{
cout << " " << p.first;
});
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment