fork download
  1. #include <iostream>
  2. #include <set>
  3.  
  4. using namespace std;
  5. typedef long long int ll;
  6.  
  7. int main() {
  8. // Optimize standard I/O operations for performance
  9. ios_base::sync_with_stdio(false);
  10. cin.tie(NULL);
  11.  
  12. ll n;
  13. if (!(cin >> n)) return 0;
  14.  
  15. multiset<ll> k;
  16. for (ll i = 0; i < n; i++) {
  17. ll val;
  18. cin >> val;
  19.  
  20. // lower_bound returns an iterator to the first element >= val
  21. auto it = k.lower_bound(val);
  22.  
  23. // If it's not the beginning, there is at least one element < val
  24. if (it != k.begin()) {
  25. --it; // Move back one step to get the largest element strictly < val
  26. k.erase(it); // Erase the found element
  27. }
  28.  
  29. // Insert the current element
  30. k.insert(val);
  31. }
  32.  
  33. cout << k.size() << "\n";
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5288KB
stdin
10
1 2 3 4 5 1 2 1 1 1
stdout
5