Cod sursa(job #1657599)

Utilizator DrumeaVDrumea Vasile DrumeaV Data 20 martie 2016 17:09:37
Problema Subsir 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <stdio.h>
#include <algorithm>

using namespace std;

const int Mn = 1e5 + 6;
const int oo = 1 << 30;

int n, ans;
int ar[Mn], dp[Mn], last[Mn];

int main()
{
    freopen("subsir2.in","r",stdin);
    freopen("subsir2.out","w",stdout);

    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ar[i]);

    ar[0] = -oo;
    for (int i = n; i >= 0; i--)
    {
        int mn = oo;
        dp[i] = oo;
        for (int j = i + 1; j <= n; j++)
            if (mn > ar[j] && ar[i] <= ar[j])
            {
                mn = ar[j];
                if (dp[i] > dp[j] + 1)
                   dp[i] = dp[j] + 1, last[i] = j;
              else
                if (dp[i] == dp[j] + 1 && ar[last[i]] > ar[j])
                   last[i] = j;
            }

        if (dp[i] == oo)
           dp[i] = 1, last[i] = i;
    }

    printf("%d\n", dp[0] - 1);
    int it = 0;
    while (it != last[it])
    {
        it = last[it];
        printf("%d ", it);
    }

    printf("\n");
    return 0;
}