Cod sursa(job #1645374)

Utilizator serbanSlincu Serban serban Data 10 martie 2016 12:07:23
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#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;
}