Skip to content
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

Merged
merged 1 commit into from
Jan 2, 2025
Merged

10-kokeunho #36

merged 1 commit into from
Jan 2, 2025

Conversation

kokeunho
Copy link
Collaborator

πŸ”— 문제 링크

[BOJ] κ°€μš΄λ°λ₯Ό λ§ν•΄μš” https://www.acmicpc.net/problem/1655

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1h

✨ μˆ˜λ„ μ½”λ“œ

ν•΄λ‹Ή 문제λ₯Ό κ·Έλƒ₯ μƒκ°λ‚˜λŠ”λŒ€λ‘œ ν’€μ—ˆλ”λ‹ˆ
λ©”λͺ¨λ¦¬ μ œν•œμ— 걸렀가지고 검색 찬슀 μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.
μ΅œλŒ€νž™, μ΅œμ†Œνž™μ„ μ‚¬μš©ν•΄μ„œ 쀑간값을 μ°ΎλŠ” μ½”λ“œμž…λ‹ˆλ‹€.
μ—¬κΈ°μ„œ μ΅œλŒ€νž™μ€ 쀑간값을 μ΄ν•˜μ˜ μˆ«μžλ“€μ΄ λ“€μ–΄κ°€λŠ” 것이고
μ΅œμ†Œνž™μ€ 쀑간값 초과의 μˆ«μžλ“€μ΄ λ“€μ–΄κ°‘λ‹ˆλ‹€.
μ΅œμ’…μ μœΌλ‘œ μ΅œλŒ€νž™μ˜ 제일 μœ„(루트)에 μžˆλŠ” μˆ«μžκ°€ μ€‘κ°„κ°’μž…λ‹ˆλ‹€.

1. 일단 μ΅œλŒ€νž™, μ΅œμ†Œνž™μ— 숫자λ₯Ό ν•˜λ‚˜μ”© λ²ˆκ°ˆμ•„κ°€λ©΄μ„œ λ„£μŠ΅λ‹ˆλ‹€.
  첫 번째 μž…λ ₯은 곧 κ·Έ 값이 μ€‘κ°„κ°’μ΄λ‹ˆ μ΅œλŒ€νž™μ— λ„£μŠ΅λ‹ˆλ‹€.
2. 이제 μ΅œμ†Œνž™μ— 값이 있고 μ΅œλŒ€νž™μ˜ 루트 값이 μ΅œμ†Œνž™μ˜ 루트 값보닀 크닀면
  κ·Έ 두 값을 κ΅ν™˜ν•©λ‹ˆλ‹€.
3. μ΅œμ’…μ μœΌλ‘œ μ΅œλŒ€νž™μ˜ λͺ¨λ“  값듀은 μ΅œμ†Œνž™μ˜ λ£¨νŠΈλ³΄λ‹€ μž‘κ²Œλ©λ‹ˆλ‹€.
4. 쀑간값인 μ΅œλŒ€νž™μ˜ 루트λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

λ©”λͺ¨λ¦¬ μ œν•œ λ¬Έμ œλŠ” 처음 ν’€μ–΄λ³΄λŠ”λ°
μžλ£Œκ΅¬μ‘°μ— λŒ€ν•΄ λ‹€μ‹œ 곡뢀해야겠닀고 λŠκΌˆμŠ΅λ‹ˆλ‹€.
이번 문제λ₯Ό ν†΅ν•΄μ„œ μ΅œλŒ€, μ΅œμ†Œνž™μ— λŒ€ν•΄μ„œ 배울 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a 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;
}

Base automatically changed from 9-kokeunho to main December 10, 2024 15:37
Copy link
Collaborator

@wnsmir wnsmir left a 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)으둜 λŒ€ν­ μ€„μ–΄λ“€κ²Œ λ©λ‹ˆλ‹€.

Copy link
Collaborator

@g0rnn g0rnn left a 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λ₯Ό μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

근데 ν•˜λ‚˜μ˜ μ΅œμ†Œ νž™μ„ μ‚¬μš©ν•΄μ„œ 문제λ₯Ό ν’€λ €κ³  ν•˜λ‹ˆ 계속 였λ₯˜κ°€ λ‚˜λ”κ΅°μš”. μ‹€μ‹œκ°„μœΌλ‘œ λ‹¬λΌμ§€λŠ” 값을 μ˜¬λ°”λ₯΄κ²Œ μ μš©ν•˜μ§€ λͺ»ν•΄μ„œ κ·ΈλŸ°κ±°μ˜€μŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ κ·Έλƒ₯ λ‹€λ₯ΈλΆ„ μ½”λ“œλ₯Ό 보고 ν’€μ—ˆμŠ΅λ‹ˆλ‹€γ… γ… 

μ½”λ“œκ°€ λ„ˆλ¬΄ λ˜‘κ°™μ•„μ„œ μ˜¬λ¦¬μ§„ μ•Šκ² μŠ΅λ‹ˆλ‹€. μž¬λ°ŒλŠ” 문제 κ°μ‚¬ν•©λ‹ˆλ‹Ή:)

@g0rnn g0rnn merged commit f36afbc into main Jan 2, 2025
1 check passed
@g0rnn g0rnn deleted the 10-kokeunho branch January 2, 2025 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants