Pagini recente » Cod sursa (job #2380165) | Cod sursa (job #670481) | Cod sursa (job #2636396) | Cod sursa (job #539311) | Cod sursa (job #2254839)
#include <iostream>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
typedef map<int, int> eqList;
int main()
{
int n, i, lmax = 0, l, indexmax;
int v[100003], p[100003] = {0};
map<int, eqList> lenList_ordered;
map<int, eqList>::reverse_iterator rit;
map<int, eqList>::iterator iter;
map<int, int>::iterator it;
fin >> n;
for(i = 1; i <= n; i++) fin >> v[i];
eqList aux; aux.insert(make_pair(0, 0));
lenList_ordered.insert(make_pair(0, aux));
aux.erase(0);
for(i = 1; i<= n; i++)
{
rit = lenList_ordered.rbegin();
while(p[i] == 0 && rit != lenList_ordered.rend())
{
l = (*rit).first + 1;
it = (*rit).second.lower_bound(v[i]);
if(it == (*rit).second.begin()){
if((*it).first == v[i]) p[i] = p[(*it).second];
}
else it--, p[i] = (*it).second;
rit++;
}
if(l > lmax) lmax = l, indexmax = i;
iter = lenList_ordered.find(l);
if(iter == lenList_ordered.end())
{
aux.insert(make_pair(v[i], i));
lenList_ordered.insert(make_pair(l, aux));
aux.erase(v[i]);
}
else{
it = (*iter).second.find(v[i]);
if(it == (*iter).second.end())
(*iter).second.insert(make_pair(v[i], i));
}
}
stack<int> st; i = indexmax;l = 0;
while(i != 0){ st.push(v[i]);i = p[i]; l++;}
fout << l << "\n";
while(!st.empty()) fout << st.top() << " ", st.pop();
return 0;
}