| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 데이터 통신과 컴퓨터 네트워크
- booksr.co.kr
- HANBIT Academy
- 김진홍 옮김
- (주)책만
- Noam Nisan
- 주우석
- 알고리즘
- 일기
- 밑바닥부터 만드는 컴퓨팅 시스템 2판
- C++
- 이득우
- 게임 수학
- 잡생각 정리글
- 입출력과 사칙연산
- unity6
- 이득우의 게임수학
- BOJ
- 메타버스
- 백준
- C#
- https://insightbook.co.kr/
- cyphenengine
- JavaScript
- The Elements of Computing Systems 2/E
- C
- 생능출판
- 전공자를 위한 C언어 프로그래밍
- 박기현
- hanbit.co.kr
Archives
- Today
- Total
cyphen156
백준-우선순위 큐 11286 절댓값 힙 본문
힙을 만드는데 첫 요소에 절댓값이 최소가 되는 자료를 맨 처음 요소로 저장하는 최소 힙을 만든다.
이 문제도 이전 문제인 최소 힙에서 변형하여 사용한다.
제약사항
- 1 <= N < 100,001
- if InputValue is 0 Then remove()
- else Then Push()
- InputValue is Intiger
- Allow InputValue Minus
주의 사항
없다.
CPP풀이
절댓값 힙_11286.cpp
/**
* 백준 절댓값 힙_11286
* 힙을 만드는데 첫 요소에 절댓값이 최소가 되는 자료를
* 맨 처음 요소로 저장하는 최소 힙을 만든다.
* 이 문제도 이전 문제인 최소 힙에서 변형하여 사용한다.
*
* 제한사항
*****************************************
* 1 <= N < 100,001 *
* if InputValue is 0 Then remove() *
* else Then Push() *
* InputValue is Intiger *
* Allow InputValue Minus *
*****************************************
*
*
*
* 주의
* 없다.
*
* 풀이시간 (문제 해석 + 구현)
* 1 + 10분
*/
#include <iostream>
static const int MAX_SIZE = 100001;
using namespace std;
static int N;
static int arr[MAX_SIZE] = { 0 };
static int length = 0;
void Push(int value);
void Pop();
int Top();
bool Compare(int a, int b);
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> N;
for (int i = 0; i < N; ++i)
{
int input;
cin >> input;
if (input == 0)
{
cout << Top() << '\n';
Pop();
}
else
{
Push(input);
}
}
return 0;
}
void Push(int value)
{
int index = length;
arr[length++] = value;
while (index > 0)
{
int parent = (index - 1) / 2;
// 부모가 자식보다 작으면 안내려도됨
if (!Compare(arr[index], arr[parent]))
{
break;
}
// else
// swap
int temp = arr[parent];
arr[parent] = arr[index];
arr[index] = temp;
index = parent;
}
}
void Pop()
{
if (length == 0)
{
return;
}
arr[0] = arr[length-1];
length--;
int index = 0;
while (1)
{
int left = index * 2 + 1;
int right = left + 1;
int largest = index;
if (left < length && Compare(arr[left], arr[largest]))
{
largest = left;
}
if (right < length && Compare(arr[right], arr[largest]))
{
largest = right;
}
if (largest == index)
{
break;
}
// swap
int temp = arr[index];
arr[index] = arr[largest];
arr[largest] = temp;
index = largest;
}
}
int Top()
{
if (length == 0)
{
return 0;
}
return arr[0];
}
// 앞 인자가 더 작은것이 참
bool Compare(int a, int b)
{
int lValue = abs(a);
int rValue = abs(b);
// 만약 절댓값이 똑같다면
if (lValue == rValue)
{
// 음수 리턴
return a < b;
}
// 아니라면 절댓값이 작은놈
return lValue < rValue;
}
모든 예제 코드의 소스파일은 제 개인 깃허브 레포지토리에 있습니다.
Workspace/알고리듬 풀이 at main · cyphen156/Workspace
Studying . Contribute to cyphen156/Workspace development by creating an account on GitHub.
github.com
'컴퓨터공학 > 알고리듬 풀이' 카테고리의 다른 글
| 백준-우선순위 큐 2696 중앙값 구하기 (3) | 2025.08.27 |
|---|---|
| 백준-우선순위 큐 2075 N번째 큰 수 (3) | 2025.08.27 |
| 백준-우선순위 큐 1927 최소 힙 (0) | 2025.08.26 |
| 백준-우선순위 큐 11279 최대 힙 (0) | 2025.08.26 |
| 백준-이분 탐색 12015 가장 긴 증가하는 부분 수열 2 (3) | 2025.08.26 |
