Pagini recente » Cod sursa (job #2076165) | Cod sursa (job #2742751) | Cod sursa (job #1732470) | Cod sursa (job #3293973) | Cod sursa (job #2861698)
#include <iostream>
#include <fstream>
#include <unordered_set>
using namespace std;
ifstream fin("secv.in");
ofstream fout("secv.out");
unordered_set <int> f;
const int N = 5000, inf = 1e18 + 1;
int v[N + 1], dp[N + 1], start[N + 1];
int main(){
int n, mini = inf, distincte = 0;
fin >> n;
for(int i = 1; i <= n; i++){
fin >> v[i], mini = min(mini, v[i]);
if(f.find(v[i]) == f.end()) distincte++, f.insert(v[i]);
}
for(int i = 1; i <= n; i++)
if(v[i] == mini)
dp[i] = 1, start[i] = i;
for(int i = 1; i <= n; i++){
if(v[i] != mini){
int maxi = -1, jchoice = -1;
for(int j = i - 1; j >= 1; j--)
if(v[j] < v[i] && dp[j] > maxi && start[j])
maxi = dp[j], jchoice = j;
if(jchoice != -1) dp[i] = dp[jchoice] + 1, start[i] = start[jchoice];
}
}
int ans = inf;
for(int i = 1; i <= n; i++)
if(dp[i] == distincte)
ans = min(ans, i - start[i] + 1);
fout << ans;
return 0;
}