Cod sursa(job #2704568)

Utilizator MihneaCadar101Cadar Mihnea MihneaCadar101 Data 10 februarie 2021 19:41:02
Problema Subsir 2 Scor 22
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("subsir2.in");
ofstream fout("subsir2.out");
const int nmax = 5000 + 50;

int n, a[nmax], dp[nmax], rez[nmax], p[nmax];
bool marked[nmax];

int main()
{
    fin >> n;
    for (int i = 1; i <= n; ++i) {
        fin >> a[i];

    }

    for (int i = n; i >= 1; --i) {
        marked[i] = true;
        bool ok = false;
        int aux;
        for (int j = i + 1; j <= n; ++j) {
            if (!ok) {
                if (a[j] >= a[i]) {
                    dp[i] = dp[j];
                    ok = true;
                    aux = a[j];
                    marked[j] = false;
                    p[i] = j;
                }
            }
            else if (a[j] >= a[i] && aux > a[j]) {
                if (dp[j] <= dp[i]) {
                    dp[i] = dp[j];
                    aux = a[j];
                    p[i] = j;
                    marked[j] = false;
                }
            }
        }

        dp[i] ++;

    }

    int mini = INT_MAX;
    for (int i = 1; i <= n; ++i) {
        if (marked[i] == true && dp[i] < mini) {
            mini = dp[i];
        }
    }

    fout << mini << '\n';
    int pos, aux = INT_MAX, minn;
    for (int i = 1; i <= n; ++i) {
        if (dp[i] == mini && a[i] < minn) {
            pos = i;
            minn = a[i];
        }
    }

    fout << pos << ' ';
    while (a[p[pos]]) {
        fout << p[pos] << ' ';
        pos = p[pos];
    }

    return 0;
}