Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- struct Robot {
- int id, hp;
- short dir; // 1 for right, -1 for left
- };
- void solve(const vector<Robot>& robots) {
- vector<Robot> st;
- for (const auto& robot : robots) {
- auto curr = robot;
- bool destroyed = false;
- while (!st.empty() && st.back().dir == 1 && curr.dir == -1) {
- if (curr.hp > st.back().hp) {
- curr.hp -= st.back().hp;
- st.pop_back();
- } else if (curr.hp < st.back().hp) {
- st.back().hp -= curr.hp;
- destroyed = true;
- break;
- } else {
- st.pop_back();
- destroyed = true;
- break;
- }
- }
- if (!destroyed) {
- st.push_back(curr);
- }
- }
- for (const auto& robot : st) {
- cout << robot.id << " " << robot.hp << endl;
- }
- }
- int main() {
- const vector<Robot> robots = {
- Robot{1, 2, 1},
- Robot{2, 5, -1},
- Robot{3, 8, 1},
- Robot{4, 11, 1},
- };
- solve(robots);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment