https://www.acmicpc.net/problem/2075
해당 문제는 굉장히 쉬워 보이나 그 쉬운 생각으로 풀게 되면 메모리를 초과하게 된다. 따라서 가장 큰 N개의 수 만큼만 담고 있으면 메모리를 초과할 일이 없게 된다.
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int N, num;
priority_queue<int, vector<int>, greater<>> pq;
cin >> N;
for (int i = 0; i < N * N; i++) {
cin >> num;
pq.push(num);
if (N < (int)pq.size()) pq.pop();
}
cout << pq.top() << '\n';
}
/*
문제의 메모리 제한을 고려해 우선순위 큐에 상위 N개의 값만 가지고 있는다.
*/
'CS(Computer Science)지식 > [C++][코딩 테스트] 자료구조 및 알고리즘' 카테고리의 다른 글
백준 1260번: DFS와 BFS (C++) (0) | 2024.02.02 |
---|---|
백준 11724번: 연결 요소의 개수 (0) | 2024.02.02 |
백준 1927번: 최소 힙 (C++) (0) | 2024.02.02 |
백준 1715번: 카드 정렬하기 (C++) (3) | 2024.01.31 |
백준 2948번: 2009년 (C++) (1) | 2024.01.31 |