fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_SIZE = 1000000;
  5.  
  6. int main() {
  7. int n, arr[MAX_SIZE + 1];
  8. cin >> n;
  9. for (int i = 1; i <= n; ++i) {
  10. cin >> arr[i];
  11. }
  12. int m;
  13. cin >> m;
  14. int x;
  15. int positions[m];
  16. for (int j = 0; j < m; ++j) {
  17. cin >> x;
  18. int left = 1, right = n, lastPosition = -1;
  19. while (left <= right) {
  20. int mid = left + (right - left) / 2;
  21. if (arr[mid] == x) {
  22. lastPosition = mid;
  23. left = mid + 1;
  24. } else if (arr[mid] < x) {
  25. left = mid + 1;
  26. } else {
  27. right = mid - 1;
  28. }
  29. }
  30. positions[j] = lastPosition;
  31. }
  32. for (int j = 0; j < m; ++j) {
  33. cout << positions[j] << " ";
  34. }
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5268KB
stdin
10
1 2 3 4 4 4 5 6 6 7
4
2 4 5 6
stdout
2 6 7 9