-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
14-seongwon030 #58
14-seongwon030 #58
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MinHeap์๋ "์ค์๊ฐ๋ณด๋ค ํฐ ๊ฒ๋ค๋ง", MaxHeap์๋ "์ค์๊ฐ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ๊ฒ๋ค๋ง" ๋ค์ด๊ฐ๊ฒ ํ๋ ์์ผ๋ก๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค :)
์ด๋ฐ ์์ผ๋ก ๋ฃ์ด์ฃผ๋ฉด Heapify ๊ณผ์ ์ด "๋ ํ์ ํฌ๊ธฐ๊ฐ ๋ค๋ฅด๋ฉด ํฌ๊ธฐ๋ฅผ ๋์ผํ๊ฒ ๋ง๋ ๋ค"๋ผ๋ ๋์๋ง ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
2696 ์ค์๊ฐ ๊ตฌํ๊ธฐ <- ์ด๊ฑฐ ๋๊ฐ์ ๋ฌธ์ ๋๊น ๋ ๋จนํ์ญ์
C++ ์ฝ๋
#include <iostream>
#include <queue>
#include <memory>
using namespace std;
class FMedianHeap
{
public:
FMedianHeap() {}
~FMedianHeap() = default;
void Emplace(int Value)
{
if(!MinHeap.empty() && Value < MinHeap.top())
{
MaxHeap.emplace(Value);
}
else
{
MinHeap.emplace(Value);
}
Heapify();
}
int Top()
{
return MaxHeap.size() >= MinHeap.size() ? MaxHeap.top() : MinHeap.top();
}
private:
priority_queue<int, vector<int>, greater<>> MinHeap; // ์ค์๊ฐ๋ณด๋ค ํฐ ๊ฒ๋ค๋ง ๋ค์ด๊ฐ
priority_queue<int> MaxHeap; // ์ค์๊ฐ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ๊ฒ๋ค๋ง ๋ค์ด๊ฐ
void Heapify()
{
if(MaxHeap.size() - MinHeap.size() == 2) // MaxHeap์ด MinHeap๋ณด๋ค 2๊ฐ ๋ง์ผ๋ฉด
{
MinHeap.emplace(MaxHeap.top()); // 2, 0 -> 1, 1
MaxHeap.pop();
}
else if(MinHeap.size() - MaxHeap.size() == 2)
{
MaxHeap.emplace(MinHeap.top());
MinHeap.pop();
}
}
};
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
auto MedianHeap = make_unique<FMedianHeap>();
int N; cin >> N;
while(N--)
{
int Num; cin >> Num;
MedianHeap->Emplace(Num);
cout << MedianHeap->Top() << "\n";
}
return 0;
}
์คํธ ์ญ์ ๋๊ฒ ์๊ฐํ๊ณ ๋ณผ ์ผ์ด๊ตฐ์ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ํฌ๊ธฐ ๋น๊ต ์ํ๊ณ ๋ฃ๋๊ฐ ์ถ์๋๋ฐ ๋ฃ๊ณ ๋ ๋ค์ ํ๋ฒ ๋น๊ตํด์ฃผ๋ฉด ๋๋๊ตฐ์.. ์ฝ๋๊ฐ ๊ตฐ๋๋๊ธฐ ์์ด์ ๋ณด๊ธฐ๊ฐ ํธํ๋ค์ ใ ใ
์ ์ญ์ ์๋ ์ ํ์ด๋ดค๋ ๋ฌธ์ ๋ผ ๊ฐํ๋กญ๋ค์; #include <iostream>
#include <queue>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
priority_queue<int> max_pq;
priority_queue<int, vector<int>, greater<int>> min_pq;
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
if (max_pq.size() > min_pq.size())
{
min_pq.push(n);
}
else
{
max_pq.push(n);
}
if (!max_pq.empty() && !min_pq.empty())
{
if (max_pq.top() > min_pq.top())
{
int temp_max = max_pq.top();
int temp_min = min_pq.top();
max_pq.pop();
min_pq.pop();
max_pq.push(temp_min);
min_pq.push(temp_max);
}
}
cout << max_pq.top() << "\n";
}
return 0;
} ์ ์ญ์ ๋ ์ฐ์ ์์ํ(์ฌ๊ธฐ์ ํ)์ ์ ์ธํ์ฌ ์์๋๋ก ๋ฒ๊ฐ์ ๋ฃ์ด์ฃผ๋ ๋ ํ์ top๊ฐ์ ๋น๊ตํด์ฃผ๋ฉด์ ์ถ๋ ฅํด์คฌ์ต๋๋ค. ํ์์ผ ๊ฒฝ์ฐmax_pq์ ๊ฐ์ด ์ต์ ๊ฐ์ผ๋ก ๊ฐฑ์ ๋๋ฉด์ ๊ฐ์ฅ ํฐ ๊ฐ์ด ์ฃผ์ด์ง ์์ด์์ ์ค์๊ฐ์ด ๋๊ณ ์ง์์ผ ๊ฒฝ์ฐ๋ ํ์ค ์์ ๊ฐ์ ์ถ๋ ฅํด์ฃผ๋ ์์ผ๋ก ์งํํ์์ต๋๋ค. ๋ฌธ์ ์ ์์ค์ ๋๊ฒ ๊ฐ๋จํ๊ณ ํ์
ํ๊ธฐ ์ฝ์ง๋ง ํ์ ๋ํ ๋ด์ฉ์ ์ ๋ชจ๋ฅด๋ ์ฌ๋๋ค์ ๋จ๋ฒ์ ํ๊ธฐ๋ ์ด๋ ค์์ ์ด๋์ ๋ ๋์ด๋๋ฅผ ์ฑ
์ ํ ๊ฒ ๊ฐ๋๋ผ๊ตฌ์. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฆฌ๋ทฐ ์ฝ๋ฉํธ๋ฅผ ๋ฌ์๋ฒ๋ ธ์ด๋ค;
๐ ๋ฌธ์ ๋งํฌ
๊ฐ์ด๋ฐ๋ฅผ ๋งํด์
โ๏ธ ์์๋ ์๊ฐ
30๋ถ
โจ ์๋ ์ฝ๋
heap์ฌ์ฉ
์ ๊ทผ๋ฒ
min_heap
๊ณผmax_heap
๋ฆฌ์คํธ๋ฅผ ๋ง๋ ๋ค๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ํ์ด์ฌ ํ ๋ชจ๋์ ์ต์ํ๋ง ์ง์ํ๋ค. ๊ทธ๋์ ์ต๋ํ์ ๊ตฌํํ๋ ค๋ฉด ํ์ ์์๋ก ์ซ์๋ฅผ ์ ์ฅํ์ฌ ๋ฃจํธ๋ฅผ ๊ณ์ฐํด์ผ ํ๋ค.