백준 2075 N번째 큰 수

supway·2022년 2월 20일
0

백준

목록 보기
22/62

백준 2075

그냥 우선순위 큐에 다 넣고 하나씩 pop해서 제출하면 메모리 초과가 난다.
어차피 찾는 수가 고정되어 있고 nxn 배열에서 두번째 행은 첫번째 행보다 무조건 크기 때문에 n개씩 우선순위 큐에 삽입하고 행끼리 계속해서 비교해주면 된다.

#include <bits/stdc++.h>
using namespace std;
int n, sum;
priority_queue<int,vector<int>,greater<int>> q;
int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	cin >> n;
	
	for (int i = 0; i < n; i++) {

		for (int j = 0; j < n; j++) {
			int x;
			cin >> x;

			if (q.size() != n) {
				q.push(x);
			}
			else {
				if (x > q.top()) {
					q.pop();
					q.push(x);
				}
			}

		}
	}
	cout << q.top() << '\n';

}
profile
개발잘하고싶은사람

0개의 댓글