Little Jay

[C++] 백준 17952번 과제는 끝나지 않아! 본문

알고리즘/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;
}

'알고리즘 > BOJ' 카테고리의 다른 글

[C++] 백준 1874번 스택 수열  (0) 2021.08.02
[C++] 백준 10799번 쇠막대기  (0) 2021.08.02
[C++] 백준 17608번 막대기  (0) 2021.07.30
[C++] 백준 1436번 영화감독 숌  (0) 2021.07.23
[C++] 백준 2749번 피보나치 수 3  (0) 2021.07.23
Comments