Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include<iostream>
  2. #define maxn 1000
  3.  
  4. using namespace std;
  5.  
  6. int f[maxn];
  7. int arr[maxn];
  8. int prv[maxn];
  9. int findRes(int n){
  10. if(f[n])return f[n];
  11. f[n]=1;
  12. for(int i=1;i<n;++i)if(arr[n]>=arr[i]&&f[n]<findRes(i)+1){
  13. f[n]=findRes(i)+1;
  14. prv[n]=i;
  15. }
  16. return f[n];
  17. }
  18. int main() {
  19. int n;
  20. cin>>n;
  21. for(int i=1;i<=n;++i)cin>>arr[i],f[i]=0,prv[i]=0;
  22. int res=0,pos;
  23. for(int i=1;i<=n;++i)if(res<findRes(i)){
  24. res=findRes(i);
  25. pos=i;
  26. }
  27. cout<<"Do dai lon nhat cua day la: "<<res<<endl;
  28. int *resArr=new int [n+1];
  29. int nResArr=res;
  30. do{
  31. resArr[nResArr--]=arr[pos];
  32. pos=prv[pos];
  33. }while(pos);
  34. for(int i=1;i<=res;++i)cout<<resArr[i]<<" ";
  35. return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement