문제
https://www.acmicpc.net/problem/16434
해설
stack에 담고 뒤에서 부터 풀면 쉽게 풀 수 있음
코드
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include<iostream> #include<algorithm> #include<stack> using namespace std; typedef long long ll; ll room_num, str, hp,room_type,room_str,room_hp,attack_cnt,max_hp; stack<tuple<int,int,int>> s; int main(){   ios_base::sync_with_stdio(false);   cin.tie(NULL);cout.tie(NULL);   cin >> room_num >> str;   for(int i=0;i<room_num;i++){     cin >> room_type >> room_str >> room_hp;     if(room_type == 2){       str += room_str;     }     s.push({room_type,room_str,room_hp});   }   hp = 1;   for(int i=0;i<room_num;i++){     tie(room_type,room_str,room_hp) = s.top();     s.pop();     if(room_type == 1){        attack_cnt = room_hp/str;       if(room_hp%str == 0) attack_cnt--;       hp += attack_cnt*room_str;     }     else{       str -= room_str;       hp -= room_hp;       if(hp<=0){         hp = 1;       }     }     max_hp = max(hp,max_hp);   }   cout << max_hp;   return 0; } | cs | 
'백준 알고리즘(C++)' 카테고리의 다른 글
| 백준 2098번 외판원 순회 ( C++ ) (0) | 2024.09.15 | 
|---|---|
| 백준 1072번 게임 ( C++ ) (0) | 2024.09.11 | 
| 백준 1269번 대칭 차집합 ( C++ ) (0) | 2024.09.10 | 
| 백준 7795번 먹을 것인가 먹힐 것인가 ( C++ ) (0) | 2024.09.10 | 
| 백준 6236 용돈 관리 ( C++ ) (0) | 2024.09.10 |