Cod sursa(job #2944237)

Utilizator CiuiGinjoveanu Dragos Ciui Data 22 noiembrie 2022 11:09:19
Problema Subsir crescator maximal Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <deque>
#include <vector>
#include <set>
#include <queue>
#include <bitset>
#include <limits.h>
using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");

const int MAX_SIZE = 100000;
int v[MAX_SIZE + 1];
pair<int, string> dp[MAX_SIZE + 1];

string convert(int nr) {
    string res = "";
    while (nr) {
        res += nr % 10 + '0';
        nr /= 10;
    }
    for (int i = 0; i < res.length() / 2; ++i) {
        swap(res[i], res[res.length() - i - 1]);
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    fin >> n;
    for (int i = 1; i <= n; ++i) {
        fin >> v[i];
        dp[i].first = 1;
        dp[i].second = convert(v[i]);
    }
    int maxLen = 1;
    string maxSub = "";
    for (int i = 1; i <= n; ++i) {
        for (int j = i - 1; j >= 0; --j) {
            if (v[j] < v[i] && dp[i].first < dp[j].first + 1) {
                dp[i].first = dp[j].first + 1;
                dp[i].second = dp[j].second + " " + dp[i].second;
                
            }
        }
        if (maxLen < dp[i].first) {
            maxLen = dp[i].first;
            maxSub = dp[i].second;
        }
    }
    fout << maxLen << "\n" << maxSub;
}

/*
pentru fiecare punct aflat, mai stocam un string in care stocam numerele respective.
 
 5
 24 12 15 15 19
 
 5
 240 120 150 150 190
 
 
 */