It's an algorithm that finds the mininum number of descending rows and the rows themselves of an array in O(nlogn) time complexity . Another part of the exercise is to use these descending rows to implement Patience Sort, which also has to be O(nlogn) (that is from the c = 1 part). What I don't get is where the logn part comes from in both cases.
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
ifstream f("data.in");
int main()
{
int n, v[100];
f >> n;
for (int i = 0; i < n; i++)
f >> v[i];
list<int> aux;
aux.push_back(v[0]);
list<list<int> > rows;
rows.push_back(aux);
for (int i = 1; i < n; i++)
{
int selected = 0;
for (list<list<int> >::iterator it = rows.begin(); it != rows.end(); it++)
{
if (it->back() > v[i])
{
it->push_back(v[i]);
selected = 1;
break;
}
}
if (!selected)
{
list<int> aux;
aux.push_back(v[i]);
rows.push_back(aux);
}
}
for (list<list<int> >::iterator it = rows.begin(); it != rows.end(); it++)
{
for (list<int>:: iterator it2 = it->begin(); it2 != it->end(); it2++)
cout << *it2 << " ";
cout << endl;
}
int c = 1;
if (c == 1)
{
int s[100];
for (int i = 0; i < n; i++)
{
list<list<int> >::iterator it = rows.begin();
int minim = it->back();
it++;
while (it != rows.end())
{
if (!it->empty())
if (it->back() < minim)
minim = it->back();
it++;
}
it = rows.begin();
while (it != rows.end())
{
if (it->back() == minim)
{
it->pop_back();
if (it->empty())
rows.erase(it);
break;
}
it++;
}
s[i] = minim;
}
for (int i = 0; i < n; i++)
cout << s[i] << " ";
}
}