STANAANDREY

robots linear battle

Mar 24th, 2026
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct Robot {
  6.     int id, hp;
  7.     short dir; // 1 for right, -1 for left
  8. };
  9.  
  10. void solve(const vector<Robot>& robots) {
  11.     vector<Robot> st;
  12.  
  13.     for (const auto& robot : robots) {
  14.         auto curr = robot;
  15.         bool destroyed = false;
  16.  
  17.         while (!st.empty() && st.back().dir == 1 && curr.dir == -1) {
  18.             if (curr.hp > st.back().hp) {
  19.                 curr.hp -= st.back().hp;
  20.                 st.pop_back();
  21.             } else if (curr.hp < st.back().hp) {
  22.                 st.back().hp -= curr.hp;
  23.                 destroyed = true;
  24.                 break;
  25.             } else {
  26.                 st.pop_back();
  27.                 destroyed = true;
  28.                 break;
  29.             }
  30.         }
  31.  
  32.         if (!destroyed) {
  33.             st.push_back(curr);
  34.         }
  35.     }
  36.  
  37.     for (const auto& robot : st) {
  38.         cout << robot.id << " " << robot.hp << endl;
  39.     }
  40. }
  41.  
  42. int main() {
  43.     const vector<Robot> robots = {
  44.         Robot{1, 2, 1},
  45.         Robot{2, 5, -1},
  46.         Robot{3, 8, 1},
  47.         Robot{4, 11, 1},
  48.     };
  49.     solve(robots);
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment