Skip to content

Instantly share code, notes, and snippets.

@vlakam
Created January 16, 2014 20:14
Show Gist options
  • Save vlakam/8462594 to your computer and use it in GitHub Desktop.
Save vlakam/8462594 to your computer and use it in GitHub Desktop.
Задача ИТМО. Второй отборочный тур.
#include <iostream>
#include <ostream>
#include <fstream>
#include <cmath>
#include <ctime>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;
std::pair<int,std::vector<int> > result;
typedef std::vector<int> vect;
typedef std::vector<int>::iterator it;
void pancake_sort(it first, it last)
{
if (std::distance(first, last) < 2) return;
for (; first != last; --last)
{
it mid = std::max_element(first, last,std::less<int>());
if (mid == last - 1)
{
continue;
}
if (first != mid)
{
++result.first;
result.second.push_back(mid-first+1);
std::reverse(first, mid + 1);
}
std::reverse(first, last);
++result.first;
result.second.push_back(last-first);
}
}
int main()
{
vector<int> x;
fstream in("input.txt",fstream::in);
fstream out("output.txt",fstream::trunc|fstream::out);
int size;
in>>size;
x.reserve(size);
for(int i=0,l=size;i<l;i++)
{
in>>size;x.push_back(size);
}
pancake_sort(x.begin(),x.end());
out<<result.first<<endl;
copy(result.second.begin(),result.second.end(),std::ostream_iterator<int>(out," "));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment