Pagini recente » Cod sursa (job #1001112) | Cod sursa (job #1146476) | Cod sursa (job #1728210) | Cod sursa (job #1760315) | Cod sursa (job #1645374)
#include <fstream>
#include <stack>
#define zero(x) ((x ^ (x - 1)) & x)
using namespace std;
stack<int> s;
int t[100005];
int a[100005], n;
pair<int, int> h[100005];
int main()
{
ifstream f("scmax.in");
ofstream g("scmax.out");
int maxim = 0, poz; f >> n;
for(int i = 1; i <= n; i ++) f >> a[i];
for(int i = 1; i <= n; i ++) {
int mx = 0, aux = 0;
for(int j = i - 1; j; j -= zero(j)) {
if(h[j].second < a[i] && mx <= h[j].first)
mx = h[j].first, aux = j;
}
mx = h[aux].first + 1;
t[i] = aux;
for(int j = i; j <= n; j += zero(j))
if(h[j].first < mx) {
h[j].first = mx;
h[j].second = a[i];
}
else if(h[j].first == mx) h[j].second = min(h[j].second, a[i]);
if(mx > maxim) {
maxim = mx;
poz = i;
}
}
g << maxim << "\n";
while(poz != 0) {
s.push(poz);
poz = t[poz];
}
while(!s.empty()) g << a[s.top()] << " ", s.pop();
g << "\n";
return 0;
}