Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #define maxn 1000
- using namespace std;
- int f[maxn];
- int arr[maxn];
- int prv[maxn];
- int findRes(int n){
- if(f[n])return f[n];
- f[n]=1;
- for(int i=1;i<n;++i)if(arr[n]>=arr[i]&&f[n]<findRes(i)+1){
- f[n]=findRes(i)+1;
- prv[n]=i;
- }
- return f[n];
- }
- int main() {
- int n;
- cin>>n;
- for(int i=1;i<=n;++i)cin>>arr[i],f[i]=0,prv[i]=0;
- int res=0,pos;
- for(int i=1;i<=n;++i)if(res<findRes(i)){
- res=findRes(i);
- pos=i;
- }
- cout<<"Do dai lon nhat cua day la: "<<res<<endl;
- int *resArr=new int [n+1];
- int nResArr=res;
- do{
- resArr[nResArr--]=arr[pos];
- pos=prv[pos];
- }while(pos);
- for(int i=1;i<=res;++i)cout<<resArr[i]<<" ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement