알고리즘/BOJ
[C++] 백준 17952번 과제는 끝나지 않아!
Jay, Lee
2021. 8. 2. 15:19
stack 자료구조를 사용하면 쉽게 풀 수 있는 문제
문제를 풀다가 utility 헤더에 있는 make_pair을 처음에 이용했는데
이게 segmentation error를 발생시켰다
구글링을 해보니까 이게 허용되지 않은 메모리 영역에 접근을 시도하거나, 허용되지 않은 방법으로 메모리 영역에 접근을 시도할 경우 발생한다고 한다.
왜 발생하는지는 모르겠지만,make_pair을 사용하는 것을 포기하고 대괄호로 묶에서 push하는 방법으로 하니까 맞았다.고수님들 계시면 이게 왜 발생하는지 알려주시면 너무 감사하겠습니다......
#include <iostream>
#include <stack>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
stack<pair<int, int>> st;
int n, total_score = 0;
int x, score, seconds;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
if (x == 1) {
cin >> score >> seconds;
st.push({seconds, score});
}
if (!st.empty()) {
st.top().first--;
if (st.top().first == 0) {
total_score += st.top().second;
st.pop();
}
}
}
cout << total_score << "\n";
return 0;
}