Advertisement
sherry_ahmos

next element

Feb 26th, 2023
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <set>
  7. #include <algorithm>
  8. #include <bits/stdc++.h>
  9.  
  10. using namespace std;
  11.  
  12. #define ll long long
  13. #define cy cout << "YES\n"
  14. #define cn cout << "NO\n"
  15. #define nl "\n"
  16. #define fi first
  17. #define se second
  18. #define MOD 1000000007
  19. #define inf 2000000000
  20. #define PI acos(-1)
  21. #define all(v) v.begin(), v.end()
  22. constexpr ll LINF = 1LL << 62;
  23. #define sz(s) s.size()
  24. #define f0r(j, n) for (ll i = j; i < n; i++)
  25. #define cin_2d(vec, n, m)                               \
  26.     for (int i = 0; i < n; i++)                         \
  27.         for (int j = 0; j < m && cin >> vec[i][j]; j++) \
  28.             ;
  29. #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0)
  30.  
  31. template <typename T = int>
  32. istream &operator>>(istream &in, vector<T> &v)
  33. {
  34.     for (auto &x : v)
  35.         in >> x;
  36.     return in;
  37. }
  38.  
  39. template <typename T = int>
  40. ostream &operator<<(ostream &out, const vector<T> &v)
  41. {
  42.     for (const T &x : v)
  43.         out << x << " ";
  44.     return out;
  45. }
  46.  
  47. void sherry()
  48. {
  49.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  50. #ifndef ONLINE_JUDGE
  51.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  52. #endif
  53. }
  54.  
  55. vector<int> next_element(vector<int> &v)
  56. {
  57.     int n = v.size();
  58.     vector<int> ans(n, -1);
  59.     stack<int> s;
  60.     for (int i = n - 1; i >= 0; i--)
  61.     {
  62.         while (!s.empty() && v[s.top()] <= v[i])
  63.             s.pop();
  64.         ans[i] = (s.empty() ? -1 : s.top() + 1);
  65.         s.push(i);
  66.     }
  67.     return ans;
  68. }
  69. void solve()
  70. {
  71.     int n;
  72.     cin >> n;
  73.     vector<int> v(n);
  74.     cin >> v;
  75.     vector<int> ans = next_element(v);
  76.     int q;
  77.     cin >> q;
  78.     while (q--)
  79.     {
  80.         int num;
  81.         cin >> num;
  82.         cout << ans[num - 1] << nl;
  83.     }
  84. }
  85. int main()
  86. {
  87.     sherry();
  88.     int t = 1;
  89.     // cin >> t;
  90.     while (t--)
  91.     {
  92.         solve();
  93.     }
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement