Advertisement
Sekai02

Lord of the Rings

Nov 25th, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. //C++17
  2.  
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. // Function to simulate the proccess and
  9. // find the lords of the ring
  10. vector<int> find_lords_of_the_ring(int n, vector<int> potential)
  11. {
  12.     bool changes = true; // Changes flag
  13.     while (changes)      // While there are still possible changes(removals) in the future
  14.     {
  15.         n = potential.size();      // Recalculate the size of the potential vector
  16.         vector<int> rem(n, false); // Keep track if elements are removed in this round
  17.         changes = false;           // Reset the changes flag
  18.  
  19.         for (int i = 0; i < n - 1; i++)
  20.         {
  21.             // If are in opposite directions
  22.             if (potential[i] >= 0 && potential[i + 1] < 0)
  23.             {
  24.                 if (abs(potential[i]) == abs(potential[i + 1])) // If the absolute values are equal, remove both
  25.                 {
  26.                     rem[i] = true;
  27.                     rem[i + 1] = true;
  28.                 }
  29.                 else if (abs(potential[i]) > abs(potential[i + 1])) // If the absolute value of the first is greater, remove the second
  30.                     rem[i + 1] = true;
  31.                 else // If the absolute value of the second is greater, remove the first
  32.                     rem[i] = true;
  33.  
  34.                 changes = true; // There was a change
  35.             }
  36.         }
  37.  
  38.         if (!changes) // If there are no changes,
  39.             break;
  40.  
  41.         // Set the new potential vector for the next round
  42.         vector<int> new_potential;
  43.         for (int i = 0; i < n; i++)
  44.             if (!rem[i])
  45.                 new_potential.push_back(potential[i]);
  46.  
  47.         // Update the potential vector
  48.         potential = new_potential;
  49.     }
  50.  
  51.     return potential;
  52. }
  53.  
  54. int main()
  55. {
  56.     // Read input
  57.     int n;
  58.     cin >> n;
  59.  
  60.     vector<int> potential(n);
  61.     for (int &p : potential)
  62.         cin >> p;
  63.  
  64.     // Find the answer and print it
  65.     for (int p : find_lords_of_the_ring(n, potential))
  66.         cout << p << " ";
  67.     cout << endl;
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement