Cod sursa(job #2851048)

Utilizator SlavicGGuzun Veaceslav SlavicG Data 17 februarie 2022 23:10:33
Problema Subsir crescator maximal Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include "bits/stdc++.h"
using namespace std;
 
#define ll long long
 
#define       forn(i,n)              for(int i=0;i<n;i++)
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()
 
#define            pb                push_back
#define          sz(a)               (int)a.size()

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    multiset<int> m; 
    vector<int> answer;
    map<int,int> pos;
    for(int i = 0;i < n; ++i) {
        cin >> a[i];
        auto it = m.lower_bound(a[i]);
        if(it == m.end()) {
            answer.pb(a[i]);
            pos[a[i]] = sz(answer) - 1;
        } else {
            pos[a[i]] = pos[*it];
            answer[pos[*it]] = a[i];
            m.erase(m.find(*it));
        }
        m.insert(a[i]);
    } 
    cout << sz(answer) << "\n";

    for(auto x: answer) {
        cout << x << " ";
    }
} 
 
int32_t main() {
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    //cin >> t;
    while(t--) {
        solve();
    }
}