Cod sursa(job #2064510)

Utilizator Alex18maiAlex Enache Alex18mai Data 12 noiembrie 2017 14:22:57
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream cin("scmax.in");
ofstream cout("scmax.out");

int n;
int nr[100100];
int dp[100100];
int intru[100100];
const int inf = 2e9 + 5;

void caut_bin(int pos){

    int st = 0 , dr = n;
    int ans = 0;

    while (st != dr){

        int mij = st + dr;
        mij /= 2;

        if (dp[mij] != inf && nr[dp[mij]] < nr[pos]){
            ans = mij;
            st = mij + 1;
        }
        else{
            dr = mij;
        }

    }

    dp[ans + 1] = pos;
    intru[pos] = dp[ans];
    //cout<<ans + 1<<" "<<pos<<'\n';

}


int main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n;

    for (int i=1; i<=n; i++){
        dp[i] = inf;
    }

    for (int i=1; i<=n; i++){
        cin>>nr[i];
        caut_bin(i);
    }

    vector <int> ans;

    for (int i=n; i>=1; i--){
        if (dp[i] != inf){
            cout<<i<<'\n';

            int last = dp[i];

            while (last != 0){
                ans.push_back(nr[last]);
                last = intru[last];
            }

            for (int j=ans.size() - 1; j>=0; j--){
                cout<<ans[j]<<" ";
            }

            break;
        }
    }


    return 0;
}