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 |
Tags
- 스택의 특징
- 풀이
- heap
- 해시 구현
- 1764
- 자료구조
- qorwns
- 백준 2447
- 백준 17779
- 구현
- AVL 시간 복잡도
- 백준 1158
- 조세퍼스 순열
- 해시구현
- dfs
- 버킷 정렬
- 원판 돌리기
- 백준 5397
- 게리멘더링2
- 시간 복잡도
- ㅣ풀이
- C/C++ 구현
- Stack 이란
- 백준
- 5397
- 백준 17471
- 백준 17822
- 백준 1406
- c#
- 별 찍기 10
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 1655] 가운데를 말해요 본문
https://www.acmicpc.net/problem/1655
요구사항
입력 수 중 중간 값 출력하기
풀이
힙을 두개 만들어 중간 값은
max힙의 값이다
1. 수가 홀수 일 때, 중간 값은 maxheap의 탑
2. 수가 짝수 일 때, 중간 값은 maxheap의 탑
그러므로 maxheap의 사이즈가 minheap의 사이즈보다 같거나 +1이여야 한다
maxheap과 minheap의 top이 교환 되었을 경우 두 수를 바꾸어 준다
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
using namespace std;
int n;
vector<int> v;
priority_queue<int> maxq; //최대 힙
priority_queue<int, vector<int>, greater<int>> minq; //최소 힙
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
if (maxq.size() == minq.size()) {
maxq.push(x);
}
else {
minq.push(x);
}
if (maxq.size() != 0 && minq.size() != 0 && maxq.top() > minq.top()) {
int maxvalue = maxq.top();
int minvalue = minq.top();
maxq.pop();
minq.pop();
maxq.push(minvalue);
minq.push(maxvalue);
}
printf("%d\n", maxq.top());
}
}
아래 블로그 참조하였다
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 1377] 버블 소트 (0) | 2020.02.29 |
---|---|
[백준 1715] 카드 정렬하기 (0) | 2020.02.29 |
[백준 2250] 트리의 높이와 너비 (0) | 2020.02.29 |
[백준 2110] 공유기 설치 (0) | 2020.02.19 |
[백준 2512] 예산 (0) | 2020.02.19 |
Comments