Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * author: vulkan
- * created: 27.07.2021 06:36:32 PM
- **/
- #include <bits/stdc++.h>
- using namespace std;
- vector<int> asteroidCollision(vector<int> &asteroids) {
- int n = asteroids.size();
- deque<int> st;
- vector<int> ans;
- for (int i = 0; i <= n - 1; ++i) {
- if (asteroids[i] > 0) {
- st.push_back(asteroids[i]);
- } else {
- while (not st.empty() and st.back() < abs(asteroids[i])) {
- st.pop_back();
- }
- if (not st.empty()) {
- if (st.back() == abs(asteroids[i])) {
- st.pop_back();
- }
- } else {
- ans.push_back(asteroids[i]);
- }
- }
- }
- while (not st.empty()) {
- ans.push_back(st.front());
- st.pop_front();
- }
- return ans;
- }
- int main(int argc, char const *argv[]) {
- int n;
- cin >> n;
- vector<int> asteroids(n);
- for (int i = 0; i <= n - 1; ++i) {
- cin >> asteroids[i];
- }
- vector<int> ans = asteroidCollision(asteroids);
- for (int x : ans) {
- cout << x << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement