commit 8e6988fa09d48111bed9bcbb4f7baa9343950cf7 parent 6115f7b029e960ac888b3fe7b0ca7a8ada099258 Author: superpozycja <anna@superpozycja.net> Date: Sun, 3 Nov 2024 23:49:04 +0100 solve nearest smaller values Diffstat:
A | sorting_and_searching/nearest_smaller_values.cpp | | | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 43 insertions(+), 0 deletions(-)
diff --git a/sorting_and_searching/nearest_smaller_values.cpp b/sorting_and_searching/nearest_smaller_values.cpp @@ -0,0 +1,43 @@ +#include <bits/stdc++.h> + +using namespace std; + +using ui = unsigned int; +using l = long; +using ul = unsigned long; +using ll = long long; +using ull = unsigned long long; + +using vi = vector<int>; +using vui = vector<ui>; +using vl = vector<l>; +using vul = vector<ul>; +using vll = vector<ll>; +using vull = vector<ull>; + +void solve() +{ + int n; + cin >> n; + stack<pair<int, int>> m; + for (int i = 0; i < n; i++) { + int x; + cin >> x; + while (!m.empty() && m.top().first >= x) + m.pop(); + if (m.empty()) + cout << "0 "; + else + cout << m.top().second << " "; + + m.push({x, i + 1}); + } + cout << "\n"; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + solve(); +}