Skip to content

Conversation

@Fnhid
Copy link
Collaborator

@Fnhid Fnhid commented Jul 12, 2025

πŸ”— 문제 링크

κ°•μ˜μ‹€

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

ν•  만 ν•΄ λ³΄μ˜€μ§€λ§Œ 이런 λ¬Έμ œκ°€ μ΅μˆ™ν•˜μ§€ μ•Šμ•„μ„œ κ·ΈλŸ°μ§€
아이디어λ₯Ό λ– μ˜¬λ¦¬κΈ°κ°€ 쉽지 μ•Šλ„€μš”,,

40λΆ„ 쯀에 κ΅¬κΈ€λ§μœΌλ‘œ 힌트λ₯Ό μ–»μ–΄ 총 1μ‹œκ°„ μ†Œμš”ν•˜μ—¬ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

문제 아이디어λ₯Ό 잘λͺ» μƒκ°ν•΄μ„œ μ‚½μ§ˆμ„ 였래 ν–ˆμŠ΅λ‹ˆλ‹€

image γ… γ… 

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

κ°•μ˜μ˜ κ°œμˆ˜κ°€ μ£Όμ–΄μ§€κ³ ,
각 κ°•μ˜μ˜ 인덱슀, μ‹œμž‘ μ‹œκ°„, μ’…λ£Œ μ‹œκ°„μ΄ μ£Όμ–΄μ§‘λ‹ˆλ‹€.

이 λ•Œ κ°•μ˜λ₯Ό κ²ΉμΉ˜μ§€ μ•Šκ²Œ κ°•μ˜μ‹€μ„ λ°°μ •ν•˜λ €λ©΄ μ΅œμ†Œ λͺ‡ 개의 κ°•μ˜μ‹€μ„ λ°°μ •ν•΄μ•Ό ν•˜λŠ”μ§€ μ•Œμ•„λ‚΄λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

λ¨Όμ € κ°•μ˜λ₯Ό μ‹œμž‘ μ‹œκ°„μ΄ λΉ λ₯Έ 순으둜 μ •λ ¬ν•©λ‹ˆλ‹€.
image

μš°μ„ μˆœμœ„ 큐

μš°μ„ μˆœμœ„ νμ—μ„œ 각 μ›μ†ŒλŠ” μš°μ„ μˆœμœ„λ₯Ό κ°€μ§€λŠ”λ°,
μš°μ„ μˆœμœ„κ°€ 높은 μ›μ†Œκ°€ λ¨Όμ € μ²˜λ¦¬λ©λ‹ˆλ‹€.
보톡 νž™μœΌλ‘œ κ΅¬ν˜„ν•©λ‹ˆλ‹€.
이 문제의 경우, μš°μ„ μˆœμœ„ νμ—μ„œ 'κ°€μž₯ 빨리 λΉ„λŠ” κ°•μ˜μ‹€'λ₯Ό λ¨Όμ € λΉΌμ•Ό ν•˜κΈ° λ•Œλ¬Έμ—
κ°•μ˜λ“€μ˜ μ’…λ£Œ μ‹œκ°„μ„ μš°μ„ μˆœμœ„ 큐둜 κ΄€λ¦¬ν•˜κ³ ,
μ’…λ£Œ μ‹œκ°„μ΄ λΉ λ₯Έ κ°•μ˜λ₯Ό μš°μ„ μˆœμœ„λ₯Ό λ†’κ²Œ λ‘‘λ‹ˆλ‹€. (μ’…λ£Œ μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ μ΅œμ†Œ νž™μ„ μ‚¬μš©ν•©λ‹ˆλ‹€.)

image

μ •λ ¬λœ μˆœμ„œλŒ€λ‘œ κ°•μ˜λ₯Ό μš°μ„ μˆœμœ„ 큐에 μ‚½μž…ν•˜λŠ”λ°, (μ‚½μž…ν•  κ°•μ˜λ₯Ό L, μš°μ„ μˆœμœ„ 큐의 top에 μžˆλŠ” κ°•μ˜λ₯Ό T라 ν•˜κ² μŠ΅λ‹ˆλ‹€)

이 λ•Œ λ§Œμ•½ T의 μ’…λ£Œ μ‹œκ°„μ΄ L의 μ‹œμž‘ μ‹œκ°„κ³Ό κ°™κ±°λ‚˜ μ•žμ— 있으면, 같은 κ°•μ˜μ‹€μ„ μ‚¬μš©ν•  수 μžˆμœΌλ―€λ‘œ top을 popν•΄ μ€λ‹ˆλ‹€.

이후 L의 μ’…λ£Œ μ‹œκ°„μ„ μƒˆλ‘œ pushν•©λ‹ˆλ‹€.

이λ₯Ό λ°˜λ³΅ν•˜λ©΄, κ°•μ˜κ°€ λ“€μ–΄μ˜¬ λ•Œ λ§ˆλ‹€ ν•„μš”ν•œ κ°•μ˜μ‹€μ˜ 수λ₯Ό μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

λ°”λ‘œ μš°μ„ μˆœμœ„ 큐의 μ›μ†Œμ˜ κ°œμˆ˜μž…λ‹ˆλ‹€.

λ”°λΌμ„œ, μš°μ„ μˆœμœ„ 큐의 μ›μ†Œμ˜ 개수의 μ΅œλŒ“κ°’μ„ κ΅¬ν•˜λ©΄ 문제λ₯Ό ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

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

μš°μ„ μˆœμœ„ 큐같은 μžλ£Œκ΅¬μ‘°λŠ” λ°°μ› λŠ”λ°λ„ 아직 ν™œμš©ν•˜κΈ° μ–΄λ ΅λ„€μš”.. 더 많이 ν’€μ–΄λ³΄λ©΄μ„œ μ΅μˆ™ν•΄μ Έ λ³΄κ² μŠ΅λ‹ˆλ‹€.

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ˜‘κ°™κ±°λ‚˜ λΉ„μŠ·λΉ„μŠ·ν•œ λ¬Έμ œλ“€μ„ λͺ‡λ²ˆ ν’€μ–΄λ΄μ„œ 금방 ν•΄κ²°ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
처음이라면 저도 μ‹œκ°„μ΄ κ½€ 걸렸을 것 κ°™λ„€μš”..

λ¬Έμ œμ—μ„œ κ°•μ˜ μ‹œκ°„μ˜ λ²”μœ„λ₯Ό 0이상 10μ–΅ μ΄ν•˜μ˜ μ •μˆ˜λ‘œ μ£Όμ—ˆκ³ ,
intκ°€ μ•½ +20μ–΅ ~ -20μ–΅ 크기의 μ •μˆ˜μžλ£Œν˜•μ„ λ‹€λ£° 수 μžˆμœΌλ―€λ‘œ ꡳ이 long long을 μ‚¬μš©ν•˜μ§€ μ•Šκ³ λ„ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

κ°•μ˜λ“€μ˜ μ‹œμž‘μ‹œκ°„κ³Ό μ’…λ£Œμ‹œκ°„μ„ λΉ„κ΅ν•˜λŠ” λ°˜λ³΅λ¬Έμ€ C++17λΆ€ν„° μ§€μ›ν•˜λŠ” ꡬ쑰적 바인딩(Structured binding) 을 μ‚¬μš©ν•˜μ—¬ ꡬ쑰체 없이 ν•΄κ²°ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
저도 λ°°μš΄μ§€ μ–Όλ§ˆ μ•ˆλ˜μ–΄μ„œ(μ—¬κΈ°μ„œ λ°°μ› μŠ΅λ‹ˆλ‹€!!) 생각날 λ•Œ λ§ˆλ‹€ μ¨λ¨Ήμ–΄λ³΄λŠ”μ€‘μž…λ‹ˆλ‹€ γ…Žγ…Ž

전체 λ‘œμ§μ€ λ™μΌν•œ 것 κ°™λ„€μš©.

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int N;
    cin >> N;
    vector<pair<int, int>> lectures;
    for(int i = 0; i < N; ++i) {
        int lectureNumber, start, end;
        cin >> lectureNumber >> start >> end;
        lectures.push_back({start, end});
    }

    sort(lectures.begin(), lectures.end());

    priority_queue<int, vector<int>, greater<int>> pq;

    int answer = 0;
    for(auto [start, end] : lectures) {
        while(!pq.empty() && pq.top() <= start) {
            pq.pop();
        }
        pq.push(end);
        answer = max(answer, (int)pq.size());
    }

    cout << answer << '\n';
    return 0;
}

Comment on lines +11 to +14
int max(int a, int b)
{
return a > b ? a : b;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<algorithm> 라이브러리λ₯Ό μ‚¬μš©ν•œ 경우,
두 인자λ₯Ό λΉ„κ΅ν•˜μ—¬ μ΅œλŒ“κ°’κ³Ό μ΅œμ†Ÿκ°’μ„ λ°˜ν™˜ν•˜λŠ” max() 와 min() ν•¨μˆ˜λ₯Ό μ œκ³΅ν•΄μ£ΌκΈ°λ•Œλ¬Έμ—
μ»€μŠ€ν…€ ν•¨μˆ˜ μ„ μ–Έ 없이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. νŽΈλ¦¬ν•΄μš© γ…Žγ…Ž

참고둜 max()ν•¨μˆ˜μ˜ λ‚΄λΆ€ μ½”λ“œ ꡬ쑰가 μœ ν˜„λ‹˜μ΄ μ„ μ–Έν•˜μ‹  μ»€μŠ€ν…€ ν•¨μˆ˜μ™€ λ™μΌν•©λ‹ˆλ‹€ γ…‹γ…‹γ…‹

@Fnhid
Copy link
Collaborator Author

Fnhid commented Jul 13, 2025

였호 ꡬ쑰적 λ°”μΈλ”©μ΄λΌλŠ” 문법이 μžˆκ΅°μš”..!!! μœ μš©ν•œ 것 κ°™λ„€μš”

저도 λ‹€μŒμ— 써 보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.

+ μš”μ¦˜ 정신이 μ—†μ–΄μ„œ.. int λ²”μœ„κ°€ ν—·κ°ˆλ¦¬λŠ” 지경에 이λ₯΄λ €λ„€μš”..γ…‹γ…‹γ…‹γ…‹ μˆ˜μ •ν•˜κ² μŠ΅λ‹ˆλ‹€πŸ« 

@froglike6
Copy link
Collaborator

μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•˜λŠ” λ‹€λ₯Έ 문제λ₯Ό ν’€μ–΄λ³Έ 적 μžˆμ–΄μ„œ, κ·Έ μ½”λ“œλ₯Ό μ‚΄νŽ΄λ³΄λ©° 기얡을 λ˜μ§šμ–΄λ³΄λ©° 이 문제λ₯Ό λΉ λ₯΄κ²Œ ν•΄κ²°ν•΄λ΄€μŠ΅λ‹ˆλ‹€. μ‹œμž‘ μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ ν›„ heap[0]<=start이면 κ·Έ κ°•μ˜μ‹€μ„ μž¬μ‚¬μš©ν•˜κ³ , μ•„λ‹ˆλ©΄ μƒˆ κ°•μ˜μ‹€μ„ μ‚¬μš©ν•˜λŠ” λ°©μ‹μœΌλ‘œ 이 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

import sys, heapq
input = sys.stdin.readline

n = int(input())
lecture = []
for _ in range(n):
    a,b,c = map(int,input().split())
    lecture.append((a,b,c))
lecture.sort(key=lambda x:x[1])
heap=[]
for _, start, end in lecture:
    if heap and heap[0]<=start:
        heapq.heappop(heap)
    heapq.heappush(heap, end)
print(len(heap))

Copy link
Collaborator

@hadongun hadongun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μŒμ˜€μ•„γ…–
μ–΄λ””μ„œ λ³Έ 문제 같은데.. 기얡이 잘 μ•ˆλ‚˜μ„œ 이것저것 μ°Έκ³ ν–ˆμŠ΅λ‹ˆλ‹€..!

μš°μ„ μˆœμœ„ 큐λ₯Ό λ°°μ› μ§€λ§Œ μ΄λŸ°μ‹μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλ‹€λŠ”κ²Œ μ°Έ μ‹ κΈ°ν•˜λ„€μš”
μ—¬λŸ¬ 뢀뢄듀을 μ ‘ν•˜λ©΄μ„œ μ’€ μ΅μˆ™ν•΄μ Έμ„œ ν™œμš© λŠ₯λ ₯을 ν‚€μ›Œλ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€!~

import sys
import heapq
input = sys.stdin.readline

n = int(input())
lecture = []
for _ in range(n):
    x, y, z = map(int, input().split())
    lecture.append((x, y, z))
lecture.sort(key = lambda x: x[1])

heap = []
time = 0

for lec in lecture:
    start = lec[1]
    end = lec[2]

    if heap and heap[0] <= start:
        heapq.heappop(heap)

    heapq.heappush(heap, end)

print(len(heap))
    

@Fnhid Fnhid merged commit 8fd27dd into main Jul 24, 2025
1 check passed
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.

5 participants