Cod sursa(job #2265816)

Utilizator caesar2001Stoica Alexandru caesar2001 Data 21 octombrie 2018 18:48:02
Problema Subsir 2 Scor 65
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("subsir2.in");
ofstream out("subsir2.out");

const int NMAX = 5005;
const int VALMAX = 1000001;

int v[NMAX], dp[NMAX], minpref[NMAX], reconst[NMAX];

struct Data {
    int x, y;
    bool operator< (Data other) const {
        if(x == other.x)
            return y < other.y;
        return x < other.x;
    }
};

bool ok(int i, int j) {
    if(i == j - 1)
        return 1;
    for(int x = i + 1; x < j; x ++)
        if(v[i] <= v[x] && v[x] <= v[j])
            return 0;
    return 1;
}

int main() {
    int n;
    in >> n;
    minpref[0] = VALMAX;
    for(int i = 1; i <= n; i ++) {
        in >> v[i];
        minpref[i] = min(v[i], minpref[i - 1]);
    }

    int scmax = NMAX, start;
    for(int i = n; i >= 1; i --) {
        int currmin = VALMAX, currmax = 0;
        dp[i] = NMAX;
        for(int j = i + 1; j <= n; j ++)
            if(v[j] >= v[i] && ok(i, j)) {
                if(dp[i] == dp[j] + 1 && v[reconst[i]] > v[j])
                    reconst[i] = j;
                if(dp[i] > dp[j] + 1) {
                    dp[i] = dp[j] + 1;
                    reconst[i] = j;
                }
            }

        if(dp[i] == NMAX)
            dp[i] = 1;
        if(scmax > dp[i] && minpref[i - 1] > v[i]) {
            scmax = dp[i];
            start = i;
        }
        if(scmax == dp[i] && minpref[i - 1] > v[i] && v[i] < v[start])
            start = i;
        //cout << i << " " << dp[i] << " " << (minpref[i - 1] > v[i]) << endl;
    }
    out << scmax << "\n";

    while(start) {
        out << start << " ";
        start = reconst[start];
    }
    return 0;
}