Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int a[1001], n;
  5. int L[1001]; // L[i] lung c.m.lung subsir cresc. care se termina cu a[i]
  6. int t[1001]; // t[i] = j (in subsir, imediat dupa a[j] s-a luat a[i]
  7.  
  8. int main()
  9. {
  10. cin >> n;
  11.  
  12. for (int i = 1; i <= n; ++i) // O(n)
  13. cin >> a[i];
  14.  
  15. for (int i = 1; i <= n; ++i)
  16. {
  17. L[i] = 1;
  18. for (int j = 1; j < i; ++j)
  19. if (a[j] < a[i] && L[i] < L[j] + 1)
  20. {
  21. L[i] = L[j] + 1;
  22. t[i] = j;
  23. }
  24. }
  25.  
  26. int Lmax = 0;
  27. int k; // poz pe care se termina cmlsc
  28.  
  29. for (int i = 1; i <= n; ++i)
  30. if (L[i] > Lmax)
  31. {
  32. Lmax = L[i];
  33. k = i;
  34. }
  35.  
  36. cout << Lmax << '\n';
  37. int m = Lmax;
  38. int rezultat[1001]
  39. for (int i = k; i > 0; i = t[i]) {
  40. rezultat[m] = a[i];
  41. --m;
  42. }
  43. for (int i = 1; i <= Lmax; ++i)
  44. cout << rezultat[i] << ' ';
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement