Pagini recente » Cod sursa (job #1818789) | Cod sursa (job #2965972) | Cod sursa (job #3284841) | Cod sursa (job #1517669) | Cod sursa (job #2265811)
#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];
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;
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))
dp[i] = min(dp[i], dp[j] + 1);
if(dp[i] == NMAX)
dp[i] = 1;
if(scmax > dp[i] && minpref[i - 1] > v[i])
scmax = dp[i];
//cout << i << " " << dp[i] << " " << (minpref[i - 1] > v[i]) << endl;
}
out << scmax << "\n";
vector<Data> reconst[NMAX + 1];
for(int i = 1; i <= n; i ++)
reconst[dp[i]].push_back({v[i], i});
for(int i = 1; i <= scmax; i ++)
sort(reconst[i].begin(), reconst[i].end());
int last = 0;
for(int step = scmax; step >= 1; step --) {
int i = 0;
for(i = 0; i < reconst[step].size() && reconst[step][i].y < last; i ++);
last = reconst[step][i].y;
out << last << " ";
}
return 0;
}