Cod sursa(job #1608321)

Utilizator dm1sevenDan Marius dm1seven Data 21 februarie 2016 23:32:50
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 1.96 kb
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <fstream>
#include <chrono>
using namespace std;
using namespace std::chrono;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<ull> vull;
typedef vector<vull> vvull;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vstring;

#define fs first
#define sc second
#define pb push_back
// forward strict for, most common
#define fors(i, a, n) for (int i = (int) (a); i < (int) (n); ++i)
// forward inclusive for
#define fori(i, a, n) for (int i = (int) (a); i <= (int) (n); ++i)
// backward for, inclusive
#define forb(i, n, a) for (int i = (int) (n); i >= (a); --i)

int main() {    
    
    ifstream in("scmax.in");
    ofstream out("scmax.out");
    int n;
    in >> n;
    vint a(n);
    fors(i, 0, n) in >> a[i];
    // vector
    // given i and elements a1, a2, ... ai. We analyse the sub-sequences of length j up to i.
    // for a sub-seqence of length j, we keep the smallest possible value a of the tail.
    // see this link for further explanations: 
    // http://www.algorithmist.com/index.php/Longest_Increasing_Subsequence
    vint sa;
    sa.push_back(a[0]);
    
    fors(i, 1, n) {
        // find j by binary search      
        // sa[j] < a[i] <= sa[j+1];
        auto it = lower_bound(sa.begin(), sa.end(), a[i]);
        if (it == sa.end()) sa.push_back(a[i]);
        else {
            int j1 = it - sa.begin();
            sa[j1] = a[i];
        }
    }
    
    out << sa.size() << endl;
    for(int v : sa) out << v << " ";
    
    return 0;
}