Cod sursa(job #653830)

Utilizator slycerdan dragomir slycer Data 28 decembrie 2011 23:38:46
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.81 kb
/* 
 * File:   Subsircrescatormaximal.cpp
 * Author: slycer
 *
 * Created on December 28, 2011, 9:18 PM
 */

#include <cstdlib>
#include <algorithm>
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;


class BIT {
public:
    int n;
    const static int MAX = 100000;
    int data[100000 + 1];
    int pos [100000 + 1];

    BIT() {
        for (int i = 0; i <= MAX; i++) {
            data[i] = 0;
        }
    }

    void update(int index, int _val, int _pos) {
        while (index <= MAX) {
            if (_val > data[index]) {
                data[index] = _val;
                pos[index] = _pos;
            }
            index += (index & -index);
        }
    }

    int readMax(int index) {
        int max = 0;
        
        while (index > 0) {
            if ( data[index]> max ){
                 max = data[index]; 
            }
            index -= (index & -index);
        }
        return  max;
    }
    
    int readPos(int index) {
        int max = 0;
        int _pos = 0; 
        while (index > 0) {
            if ( data[index]> max ){
                 max = data[index]; 
                 _pos = pos[index]; 
            }
            index -= (index & -index);
        }
        return  _pos;
    }
};

/*
 * 
 */
int main(int argc, char** argv) {
    ifstream input("scmax.in");
    ofstream output("scmax.out");
    int n;
    input >> n;
    vector<long int> data;
    vector<long int> aux;
    for (int i = 0; i < n; i++) {
        long int c;
        input >> c;
        data.push_back(c);
        aux.push_back(c);
    }
    sort(aux.begin(), aux.end());
    BIT bit; 
    int dp[n+1];
    int back[n+1];
    for (int i = 0; i < n; i++) {
        long int c = data[i];
        int a = 0;
        int b = n - 1;
        int m;
        while (a <= b) {
            m = (a + b) / 2;
            if (c == aux[m]) {
                break;
            }

            if (c < aux[m]) {
                b = m - 1;
            }

            if (c > aux[m]) {
                a = m + 1;
            }
        }
        int current = m+1;
        
        dp[i] = bit.readMax( current -1 )+1;
        back[i] = bit.readPos( current - 1 );
       // cout << dp[i] << " " << back[i] << "\n";
        bit.update( current, dp[i], i );   
    }
    
   /* for (int i = 0; i < n; i++) {
        cout << data[i] << " ";
    }*/
    
    int max=-1; 
    int maxPos=-1;
    for ( int i=0; i<n; i++){
        if ( dp[i] > max ){
            max = dp[i]; 
            maxPos = i; 
        }
    }
    output << max << "\n"; 
    vector<long int> stack;
    
    for ( int i=0; i<max; i++ ){
      //  output << data[maxPos] << " ";
        stack.push_back( data[maxPos] );
        maxPos = back[maxPos];
    }
    for ( int i=0; i<max; i++){
         output << stack.back() << " ";
         stack.pop_back();
    }

    return 0;
}