일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 인앱결제
- 코딩테스트 연습
- IT 트렌드
- 백준
- 코테
- 싸피
- SSAFY
- 네트워크 관리사
- 백준위
- 싸피셜
- KT
- it 뉴스
- html
- 리얼클래스
- it 이슈
- 구글
- 신문 스크랩
- 프로그래머스
- IT 동향
- 코딩테스트
- 우테코
- 카카오
- 신문스크랩
- 네트워크 관리사 2급 실기
- SSAFY 7기
- SSAFYcial
- 네트워크 관리사 2급
- python
- Java
- java 객체지향 프로그래밍
Archives
- Today
- Total
개발자일걸요..?
11286번 절댓값 힙 본문
728x90
반응형
문제링크 : www.acmicpc.net/problem/11286
11286번: 절댓값 힙
첫째 줄에 연산의 개수 N(1≤N≤100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 0이 아니라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
버전 1. vector를 이용하여 heap 구현(메모리 : 2800KB 시간 : 984ms)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void del(vector<long> &d) {
d[1] = d[d.size() - 1];
d.erase(d.end()-1);
int now = 1;
long mini = 0;
int miniInd = 1;
while (2*now<d.size()) {
mini = d[2 * now];
miniInd = 2 * now;
if (2 * now + 1 < d.size()) {
if (abs(d[2 * now]) == abs(d[2 * now + 1])) {
if (d[2 * now] > d[2 * now + 1]) {
mini = d[2 * now + 1];
miniInd = 2 * now + 1;
}
}
else if (abs(d[2 * now]) > abs(d[2 * now + 1])) {
mini = d[2 * now + 1];
miniInd = 2 * now + 1;
}
}
if (abs(mini) < abs(d[now])) {
long temp2 = d[now];
d[now] = mini;
d[miniInd] = temp2;
now = miniInd;
}
else if (abs(mini) == abs(d[now])) {
if (mini < d[now]) {
long temp2 = d[now];
d[now] = mini;
d[miniInd] = temp2;
now = miniInd;
}
else break;
}
else { break; }
}
}
void in(vector<long> &list, long a) {
list.push_back(a);
int child = list.size() - 1;
int parent = (int)(child / 2);
while (child > 1) {
if (abs(list[parent]) < abs(list[child])) break;
else if (abs(list[parent]) == abs(list[child])
&& list[parent] < list[child]) break;
else {
long temp = list[child];
list[child] = list[parent];
list[parent] = temp;
child = parent;
parent /= 2;
}
}
}
int main() {
int T = 0;
cin >> T;
vector<long> list;
list.push_back(0);
long a = 0;
while (T--) {
cin >> a;
if (a == 0) {
if (list.size() == 1) {
cout << "0\n";
}
else {
cout << list[1] << "\n";
del(list);
}
}
else {
in(list, a);
}
}
return 0;
}
버전 2. priority queue로 heap구현(메모리 : 2460KB 시간: 952ms)
#include<iostream>
#include<queue>
#include <algorithm>
using namespace std;
int main() {
int T = 0;
cin >> T;
int a = 0;
priority_queue<int, vector<int>, greater<int>> positive;
priority_queue<int, vector<int>, less<int>> negative;
while (T--) {
cin >> a;
if (a == 0) {
if (positive.empty() && negative.empty()) cout << "0\n";
else {
if (negative.empty()) {
cout << positive.top() << "\n";
positive.pop();
}
else if(positive.empty()) {
cout << negative.top() << "\n";
negative.pop();
}
else {
if (abs(negative.top()) < abs(positive.top())) {
cout << negative.top() << "\n";
negative.pop();
}
else if (abs(negative.top()) == abs(positive.top())) {
cout << negative.top() << "\n";
negative.pop();
}
else if (abs(negative.top()) > abs(positive.top())) {
cout << positive.top() << "\n";
positive.pop();
}
}
}
}
else {
if (a > 0) positive.push(a);
else negative.push(a);
}
}
return 0;
}
반응형
'알고리즘코딩 > Baekjoon Online Judge' 카테고리의 다른 글
1932번 정수 삼각형 (0) | 2021.03.10 |
---|---|
1655번 가운데를 말해요 (0) | 2021.03.09 |
1463번 1로 만들기 (0) | 2021.03.07 |
1927번 최소 힙 (0) | 2021.03.06 |
11279번 최대 힙 (0) | 2021.03.05 |
Comments