Pagini recente » Cod sursa (job #2585278) | Cod sursa (job #1476950) | Cod sursa (job #1723836) | Cod sursa (job #2424909) | Cod sursa (job #3284892)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("secv.in");
ofstream fout("secv.out");
#define MAXN 5000
int v[MAXN + 1];
int alt[MAXN + 1];
int d[MAXN + 1];
int st[MAXN + 1];
int main()
{
int N, i, j, o = 1, maxi, rez = -1;
fin >> N;
for (i = 1; i <= N; i++)
{
fin >> v[i];
alt[i] = v[i];
}
sort(alt + 1, alt + N + 1);
i = 1;
while (i < N)
{
if (alt[i] != alt[i + 1])
{
o++;
}
i++;
}
for (i = 1; i <= N; i++)
{
j = i - 1;
maxi = 0;
while (j > 0)
{
if (v[j] < v[i])
{
if (maxi == 0)
{
maxi = j;
}
else if (d[maxi] < d[j])
{
maxi = j;
}
}
j--;
}
if (maxi == 0)
{
st[i] = i;
d[i] = 1;
}
else
{
d[i] = d[maxi] + 1;
st[i] = st[maxi];
}
}
for (i = 1; i <= N; i++)
{
if (d[i] == o)
{
if (rez == -1)
{
rez = i - st[i] + 1;
}
else if (rez > i - st[i] + 1)
{
rez = i - st[i] + 1;
}
}
}
fout << rez;
return 0;
}