Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <map>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. #define M 1048576
  8. #define treeSize 2097152
  9. int maxTree[treeSize];
  10. int dyn[M];
  11. int tab[M];
  12. int n;
  13. void insertion(int where, int val)
  14. {
  15. where += M;
  16. maxTree[where] = val;
  17. where /=2;
  18. while (where)
  19. {
  20. maxTree[where] = max(maxTree[2*where], maxTree[2*where+1]);
  21. where /=2;
  22. }
  23. }
  24.  
  25. int getMax (int a, int b)
  26. {
  27. a += M;
  28. b += M;
  29.  
  30. int ans = max(maxTree[a], maxTree[b]);
  31. while (a/2 != b/2)
  32. {
  33. if (a%2 == 0) ans = max(ans,maxTree[a+1]);
  34. if (b%2 == 1) ans = max(ans,maxTree[b-1]);
  35. a /= 2;
  36. b /= 2;
  37. }
  38.  
  39. return ans;
  40. }
  41.  
  42. int main()
  43. {
  44. for (int i=0; i!=treeSize; ++i) maxTree[i] = 0;
  45. cin >> n;
  46.  
  47. set <int> St;
  48. map <int,int> Mp;
  49. vector <int> V;
  50. for (int i=0; i!=n; ++i)
  51. {
  52. cin >> tab[i];
  53. St.insert(tab[i]);
  54. }
  55.  
  56. int val = 0;
  57. for (auto i:St) V.push_back(i);
  58. sort(V.begin(), V.end());
  59. for (int i=0; i<V.size(); ++i) Mp[V[i]] = ++val;
  60. for (int i=0; i!=n; ++i) tab[i] = Mp[tab[i]];
  61.  
  62. for (int i=0; i!=n; ++i)
  63. {
  64. dyn[i] = getMax(0,tab[i]-1)+1;
  65. insertion(tab[i], dyn[i]);
  66. }
  67.  
  68. int odp = 0;
  69. for (int i=0; i!=n; ++i) odp = max(odp,dyn[i]);
  70. cout << odp;
  71.  
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement