Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //C++17
- #include <iostream>
- #include <vector>
- using namespace std;
- // Function to simulate the proccess and
- // find the lords of the ring
- vector<int> find_lords_of_the_ring(int n, vector<int> potential)
- {
- bool changes = true; // Changes flag
- while (changes) // While there are still possible changes(removals) in the future
- {
- n = potential.size(); // Recalculate the size of the potential vector
- vector<int> rem(n, false); // Keep track if elements are removed in this round
- changes = false; // Reset the changes flag
- for (int i = 0; i < n - 1; i++)
- {
- // If are in opposite directions
- if (potential[i] >= 0 && potential[i + 1] < 0)
- {
- if (abs(potential[i]) == abs(potential[i + 1])) // If the absolute values are equal, remove both
- {
- rem[i] = true;
- rem[i + 1] = true;
- }
- else if (abs(potential[i]) > abs(potential[i + 1])) // If the absolute value of the first is greater, remove the second
- rem[i + 1] = true;
- else // If the absolute value of the second is greater, remove the first
- rem[i] = true;
- changes = true; // There was a change
- }
- }
- if (!changes) // If there are no changes,
- break;
- // Set the new potential vector for the next round
- vector<int> new_potential;
- for (int i = 0; i < n; i++)
- if (!rem[i])
- new_potential.push_back(potential[i]);
- // Update the potential vector
- potential = new_potential;
- }
- return potential;
- }
- int main()
- {
- // Read input
- int n;
- cin >> n;
- vector<int> potential(n);
- for (int &p : potential)
- cin >> p;
- // Find the answer and print it
- for (int p : find_lords_of_the_ring(n, potential))
- cout << p << " ";
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement