Cod sursa(job #1331554)

Utilizator cerrahoglu.aliAli Cerrahoglu cerrahoglu.ali Data 31 ianuarie 2015 19:58:35
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.74 kb
//
//  main.cpp
//  scmax
//
//  Created by Ali Cerrahoglu on 30/01/15.
//  Copyright (c) 2015 Ali Cerrahoglu. All rights reserved.
//

#include <iostream>
#include <fstream>
using namespace std;

const int N = 100005;

int x[N] = {0},
    y[N],
    a[N],
    previ[N] = {0};

void binarySearch( int val, int &endd, int currIndex ) {

    int first = 1,
        last = endd,
        midd;

    while( true ) {

        midd = (last+first) / 2;

        if( val > a[x[midd]] ) {
            first = midd;
        }
        else {
            last = midd;
        }

        if( last-first <= 1 ) {
            if( val > a[x[first]] ) {
                if( val < a[x[last]] ) {
                    x[last] = currIndex;
                    previ[currIndex] = first;
                }
                else if( val > a[x[last]] ) {
                    // this means last = end;
                    x[++endd] = currIndex;
                    previ[currIndex] = endd - 1;
                }
            }
            else if( val < a[x[first]] ) {
                // this means that first hasn't changed from the beggining of this function
                x[first] = currIndex;
                previ[currIndex] = false;
            }
            break;
        }
    }
}

void Print(int i){

    if (i > 0){
        Print(previ[i]);
        g << " " << a[x[i]];
    }
}

int main() {

    int n,i; int endd=1;

    ifstream f("scmax.in");
    ofstream g("scmax.out");

    f >> n;

    for ( i=1; i<=n; i++ ) {
        f >> a[i];
    }

    x[1] = 1;
    for( i=2; i<=n; i++ ) {
        binarySearch( a[i], endd, i );
    }

    i = endd;

    g << endd << "\n";

    Print(i);

    return 0;
}