Pagini recente » Cod sursa (job #283768) | Cod sursa (job #598844) | Cod sursa (job #2056136) | Cod sursa (job #2399331) | Cod sursa (job #508710)
Cod sursa(job #508710)
using namespace std;
#include<iostream>
#include<fstream>
#include<algorithm>
int a[5005],b[5005],dp[2][5005],c[5005],N,M,rez;;
ofstream fout("secv.out");
void solve()
{int i,j,p,q;
dp[0][0]=0;
for(i=1;i<=N;i++) dp[0][i]=0x3f3f3f3f;
p=0;
rez=0x3f3f3f3f;
q=1;
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
{
dp[q][j]=dp[p][j]+1;
if(a[i]==c[j])
dp[q][j]=dp[p][j-1]+1;
//cout<<dp[q][j]<<" ";
}
rez=min(rez,dp[q][M]);
//cout<<"\n";
p=1-p;
q=1-q;
}
if(rez<0x3f3f3f3f)
fout<<rez<<"\n";
else fout<<"-1\n";
}
void cit()
{int i;
ifstream fin("secv.in");
fin>>N;
for(i=1;i<=N;i++)
{
fin>>a[i];
b[i]=a[i];
}
sort(b+1,b+1+N);
c[1]=b[1];
M=1;
for(i=2;i<=N;i++)
if(b[i]!=b[i-1])
c[++M]=b[i];
fin.close();
}
int main()
{
cit();
solve();
fout.close();
return 0;
}