fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using vi = vector <int>;
  5.  
  6. set <int> s;
  7.  
  8. vi foo(vi const &a) {
  9. vi ret;
  10. s.clear();
  11. for (auto const x: a) {
  12. auto it = s.lower_bound(x);
  13. if (it != begin(s)) {
  14. it = prev(it);
  15. ret.push_back(*it);
  16. } else {
  17. ret.push_back(-1);
  18. }
  19. s.insert(x);
  20. }
  21. return ret;
  22. }
  23.  
  24. int main() {
  25. vi a = {1, 2, 5, 4, 2, 6, 7, 3};
  26. vi b = foo(a);
  27. for (auto x: b) printf("%d ", x); puts("");
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
-1 1 2 2 1 5 6 2