Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 그래프
- 개발
- 컴퓨터공학과
- 북리뷰
- 오에스
- 오퍼레이팅시스템
- 브루트포스
- OS
- c++
- Operating System
- bfs
- 코딩
- 정석학술정보관
- coding
- vector
- 스택
- 문제풀이
- Computer science
- 알고리즘
- cs
- DP
- 자료구조
- 정석
- 코테
- 백준
- 컴공과
- Stack
- 구현
- 너비우선탐색
- 컴공
Archives
- Today
- Total
Little Jay
[C++] 백준 17952번 과제는 끝나지 않아! 본문
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