-
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
10-kokeunho #36
10-kokeunho #36
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.
μ λ μ²μ λ μ€λ₯Έ λ°©μλλ‘ νλκΉ μκ°μ΄κ³Όκ° λ¨λλΌκ³ μ..
κ·Έλμ κ³ λ―Όμ μ’ νλ€κ° μ°μ μμνλ₯Ό 2κ°λ₯Ό μ¨μ λ°λ° λλλ μμ΄λμ΄λ
μΌν μκ°μ ν΄λλλ° κ·Έ λ€λ‘ κ·Έλ΄μΈν ꡬνμ΄ μλμμ΅λλ€.
κ²°κ΅ μμΉ..
μκ°μ λ ν¬μνμ΄λ λͺ»νμμ κ² κ°κΈ°λ νλ€μ.
μ½λ μ체λ κ·ΌνΈλ μ½λλ μ μ¬νλ€μ
μ΅λν, μ΅μνμ κ°μ κ· νμ λ§μΆ°κ°λ©° μ½μ
νκ³ ,
μ΅λνμ topμ΄ μ΅μνμ topλ³΄λ€ ν¬λ©΄ swapν λ€ μΆλ ₯μ νλ μ½λ μ
λλ€.
CPP CODE
#include <iostream>
#include <queue>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
priority_queue<int> maxHeap;
priority_queue<int, vector<int>, greater<int>> minHeap;
int N;
cin >> N;
while (N--) {
int num;
cin >> num;
if (maxHeap.size() == minHeap.size()) maxHeap.push(num);
else minHeap.push(num);
if(!maxHeap.empty() && !minHeap.empty() && (maxHeap.top() > minHeap.top())) {
int max = maxHeap.top();
int min = minHeap.top();
maxHeap.pop();
minHeap.pop();
maxHeap.push(min);
minHeap.push(max);
}
cout << maxHeap.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.
μ²μμ λ¬Έμ λ₯Ό λ³΄κ³ μ무μκ°μμ΄
N = int(input())
num = []
for i in range(N):
num.append(int(input()))
num.sort()
print(num[int((len(num)+1)/2)-1])
μ΄λ κ² νμλ€κ° λ°λ‘ μκ°μ΄κ³Όλμ λ€μνλ² μκ°ν΄λ³΄μμ΅λλ€.
νμ΄μ¬ λ°°μ΄μ sortλ©μλλ 리μ€νΈμ κΈΈμ΄κ° kμΌλ k log kμ μκ°μ΄ 걸립λλ€.
κ±°κΈ°μ λ°λ³΅λ¬ΈκΉμ§ λλ©΄ μ§μμΉμ μκ°λ³΅μ‘λκ° λμ€κΈ° λλ¬Έμ μλΉν λΉν¨μ¨μ μΈ λ°©μμ΄μμ΅λλ€.
λ°λΌμ κ³ λ―Όν΄λ³΄λ€ μ λ ¬λ μνλ‘ μ½μ μ νλ€λ©΄ λ§€λ² λͺ¨λ μμλ€μ μ λ ¬ν νμκ° μλ€κ³ νλ¨νμ΅λλ€.
import bisect
import sys
input = sys.stdin.readline # λΉ λ₯Έ μ
λ ₯μ μν΄ sys.stdin.readline μ¬μ©
N = int(input()) # 첫 λ²μ§Έ μ€μμ μ«μ κ°μ μ
λ ₯
numbers = []
for _ in range(N):
num = int(input()) # ν μ€μ© μ
λ ₯λ°μ
bisect.insort(numbers, num) # μ λ ¬λ μνλ‘ μ½μ
print(numbers[(len(numbers) - 1) // 2]) # μ€κ°κ° κ³μ°
κΈ°μ‘΄μλ μλ₯Ό κ·Έλ₯ μΆκ°νκ³ μ λ ¬μ νλ€λ©΄,
μ κ°μ μ½μ
ν μμΉλ§ μ΄μ§νμμΌλ‘ μ°Ύκ³ , μ½μ
μμ
λ§ μνν΄μ£Όλ©΄ λ©λλ€.
μ¦ μκ°λ³΅μ‘λκ°
O(N^3 logN)μμ O(N^2)μΌλ‘ λν μ€μ΄λ€κ² λ©λλ€.
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.
골λ2λΌ μκ³ λ¦¬μ¦μ μΌλ‘ μ΄λ €μΈμ€ μμλλ° μ¬λ°λ₯Έ μ루μ
μ μκ°νκΈ°κ° μ΄λ €μ΄ λ¬Έμ μλ€μ. μ λ μ²μμ multisetλ₯Ό μ¬μ©ν΄μ νλ €κ³ νμΌλ μκ°μ΄κ³Όκ° λμ priority_queue
λ₯Ό μ¬μ©νμ΅λλ€.
κ·Όλ° νλμ μ΅μ νμ μ¬μ©ν΄μ λ¬Έμ λ₯Ό νλ €κ³ νλ κ³μ μ€λ₯κ° λλκ΅°μ. μ€μκ°μΌλ‘ λ¬λΌμ§λ κ°μ μ¬λ°λ₯΄κ² μ μ©νμ§ λͺ»ν΄μ κ·Έλ°κ±°μμ΅λλ€. κ·Έλμ κ·Έλ₯ λ€λ₯ΈλΆ μ½λλ₯Ό λ³΄κ³ νμμ΅λλ€γ γ
μ½λκ° λ무 λκ°μμ μ¬λ¦¬μ§ μκ² μ΅λλ€. μ¬λ°λ λ¬Έμ κ°μ¬ν©λλΉ:)
π λ¬Έμ λ§ν¬
[BOJ] κ°μ΄λ°λ₯Ό λ§ν΄μ https://www.acmicpc.net/problem/1655
βοΈ μμλ μκ°
1h
β¨ μλ μ½λ
ν΄λΉ λ¬Έμ λ₯Ό κ·Έλ₯ μκ°λλλλ‘ νμλλ
λ©λͺ¨λ¦¬ μ νμ κ±Έλ €κ°μ§κ³ κ²μ μ°¬μ€ μ¬μ©νμ΅λλ€.
μ΅λν, μ΅μνμ μ¬μ©ν΄μ μ€κ°κ°μ μ°Ύλ μ½λμ λλ€.
μ¬κΈ°μ μ΅λνμ μ€κ°κ°μ μ΄νμ μ«μλ€μ΄ λ€μ΄κ°λ κ²μ΄κ³
μ΅μνμ μ€κ°κ° μ΄κ³Όμ μ«μλ€μ΄ λ€μ΄κ°λλ€.
μ΅μ’ μ μΌλ‘ μ΅λνμ μ μΌ μ(루νΈ)μ μλ μ«μκ° μ€κ°κ°μ λλ€.
π μλ‘κ² μκ²λ λ΄μ©
λ©λͺ¨λ¦¬ μ ν λ¬Έμ λ μ²μ νμ΄λ³΄λλ°
μλ£κ΅¬μ‘°μ λν΄ λ€μ 곡λΆν΄μΌκ² λ€κ³ λκΌμ΅λλ€.
μ΄λ² λ¬Έμ λ₯Ό ν΅ν΄μ μ΅λ, μ΅μνμ λν΄μ λ°°μΈ μ μμμ΅λλ€.