Pagini recente » Cod sursa (job #2469363) | Cod sursa (job #3192608) | Cod sursa (job #3211756) | Cod sursa (job #41499) | Cod sursa (job #2533395)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#define NMAX 5000
using namespace std;
ifstream fin("subsir2.in");
ofstream fout("subsir2.out");
struct el
{
int info,index;
};
void read(el *v,int &n)
{
fin>>n;
for(int i=0;i<n;++i)
{
fin>>v[i].info;
v[i].index=i+1;
}
}
void print(el *v,int n)
{
for(int i=0;i<n;++i)
cout<<v[i].info<<' '<<v[i].index<<'\n';
cout<<'\n';
}
bool cmp(el x,el y)
{
return (x.info<y.info);
}
int maxLenSeq(el *v,int n,int *final_vec)
{
int best[NMAX],prec[NMAX];
bool isSeqEnd[NMAX]={0};
for(int i=0;i<n;++i)
{
best[i]=n+1;
prec[i]=i;
isSeqEnd[i]=true;
for(int j=0;j<i;++j)
{
if(v[i].info>=v[j].info)
{
if(best[j]+1<best[i])
{
best[i]=best[j]+1;
prec[i]=j;
}
isSeqEnd[j]=false;
}
}
if(best[i]==n+1)
best[i]=1;
}
for(int i=0;i<n;++i)
cout<<best[i]<<' ';
cout<<'\n';
for(int i=0;i<n;++i)
cout<<isSeqEnd[i]<<' ';
cout<<'\n';
int k,m=n,last=0;
for(int i=0;i<n;++i)
{
if(isSeqEnd[i] && best[i]<m)
{
m=best[i];
last=i;
}
}
k=m;
while(prec[last]!=last)
{
final_vec[--k]=v[last].index;
last=prec[last];
}
final_vec[--k]=v[last].index;
return m;
}
int main()
{
srand(time(NULL));
el v[NMAX];
int n;
read(v,n);
/*n=rand()%6+7;
for(int i=0;i<n;++i)
{
v[i].info=rand()%50;
v[i].index=i+1;
}*/
print(v,n);
//sort(v,v+n,cmp);
print(v,n);
int res[NMAX],k=maxLenSeq(v,n,res);
fout<<k<<'\n';
for(int i=0;i<k;++i)
fout<<res[i]<<' ';
return 0;
}