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

11-kokeunho #43

Merged
merged 1 commit into from
Jan 6, 2025
Merged

11-kokeunho #43

merged 1 commit into from
Jan 6, 2025

Conversation

kokeunho
Copy link
Collaborator

πŸ”— 문제 링크

[BOJ] νšŒμ˜μ‹€ λ°°μ • https://www.acmicpc.net/problem/1931

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

50min

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

각 회의의 μ‹œμž‘κ³Ό 끝을 meetings 배열에 λ„£μŠ΅λ‹ˆλ‹€.
끝을 κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•˜κ³  끝이 κ°™λ‹€λ©΄ μ‹œμž‘μ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•©λ‹ˆλ‹€.
μ •λ ¬λœ 배열을 λŒλ©΄μ„œ
ν•΄λ‹Ή 회의의 μ‹œμž‘ μ‹œκ°„μ΄ 이전 회의의 끝보닀 λŠ¦λ‹€λ©΄
count 값을 μΆ”κ°€ν•˜κ³  이전 회의 끝 μ‹œκ°„μ„ κ°±μ‹ μ‹œν‚΅λ‹ˆλ‹€.
κ·Έ κ²°κ³Ό μ΅œλŒ€ 회의 개수λ₯Ό ꡬ할 수 μžˆμŠ΅λ‹ˆλ‹€.

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

문제 μžμ²΄λŠ” 어렡지 μ•Šμ•˜λŠ”λ°
μžλ°” 정렬을 아직 λͺ°λΌκ°€μ§€κ³ ... 검색 ν•΄λ΄€μŠ΅λ‹ˆλ‹€.
이번 λ¬Έμ œμ—μ„œ μ •λ ¬κ³Ό λžŒλ‹€ ν‘œν˜„μ‹μœΌλ‘œ 읡λͺ… ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜λŠ” 법도 λ°°μ› μŠ΅λ‹ˆλ‹€.

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.

μ•Œκ³ λ¦¬μ¦˜ κ°•μ˜λ•Œ 봀던 문제라 λ°˜κ°€μ› μŠ΅λ‹ˆλ‹€
문제λ₯Ό 읽고 νšŒμ˜κ°€ λλ‚˜λŠ” μ‹œκ°μ„ λΉ„κ΅ν•΄μ•Όκ² λ‹€λŠ” 생각이 λ°”λ‘œ λ– μ˜¬λžμŠ΅λ‹ˆλ‹€!

κ·Έλž˜μ„œ 회의 μ‹œμž‘κ³Ό 끝 μ‹œκ°„μ„ λ¬Άμ–΄μ„œ μ €μž₯ν•˜κ³ ,
끝 μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ν–ˆμŠ΅λ‹ˆλ‹€.

κ·Έ λ‹€μŒμ€ κ·Όν˜Έλ‹˜μ΄ μž‘μ„±ν•˜μ‹  for λ¬Έκ³Ό λ˜‘κ°™μ€ λ…Όλ¦¬λ‘œ μ§„ν–‰λ©λ‹ˆλ‹€.
(이런 λ¬Έμ œλŠ” λ‹€λ“€ μƒκ°ν•˜λŠ” 게 λΉ„μŠ·ν•΄μ„œ 풀이가 λ§Žμ΄λ“€ λΉ„μŠ·ν•œ 것 κ°™μ•„μš”)

λ¬Έμ œκ°€ μžˆμ—ˆλ‹€λ©΄ compare ν•˜λŠ” 방식을 κΉŒλ¨Ήμ—ˆλ‹€λŠ” 것....
Javaμ—μ„œλŠ” 보톡 λžŒλ‹€λ₯Ό μ¨μ„œ λΉ„κ΅ν•˜λ‚˜ λ³΄λ„€μš”
λžŒλ‹€μ‹ μ•Œκ³ λŠ” μžˆμ§€λ§Œ ν•œλ²ˆλ„ 써본적 μ—†λŠ”,,,

μ–΄μ¨Œλ“  이 λ¬Έμ œλŠ” λμ‹œκ°„ κΈ°μ€€μœΌλ‘œ ν™•μΈν•΄μ•Όν•œλ‹€λŠ” 것을 μ•Œκ³ , 정렬을 ν•  수 있으면
잘 ν’€μˆ˜μžˆλŠ” λ¬Έμ œμ˜€λ˜ 것 κ°™μŠ΅λ‹ˆλ‹€! κ°μ‚¬ν•©λ‹ˆλ‹€!

CPP CODE
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool compare(const pair<int, int>& a, const pair<int, int>& b) {
    if (a.second == b.second) return a.first < b.first;
    return a.second < b.second;
}

int main() {
    int N;
    cin >> N;

    vector<pair<int, int>> time;
    for (int i = 0; i < N; i++) {
        int s, e;
        cin >> s >> e;

        time.push_back(make_pair(s, e));
    }

    sort(time.begin(), time.end(), compare);

    int cnt = 0;
    int last = 0;
    for (pair<int, int>& t : time) {
        if (t.first >= last) {
            last = t.second;
            cnt++;
        }
    }

    cout << cnt;

    return 0;
}

Base automatically changed from 10-kokeunho to main January 2, 2025 07:23
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.

저도 κ°•λž˜λ‹˜μ²˜λŸΌ μ•Œκ³ λ¦¬μ¦˜ λ•Œ λ°°μ› λ˜ λ¬Έμ œμ—¬μ„œ μ†”λ£¨μ…˜μ΄ μ‰½κ²Œ μƒκ°λ‚¬μŠ΅λ‹ˆλ‹€. 이 문제λ₯Ό ν‘ΈλŠ” 방법은 μ „λΆ€ λΉ„μŠ·ν• κ±°λΌ μƒκ°ν•©λ‹ˆλ‹€. κ·Έλž˜μ„œ 저도 κ·Όν˜Έλ‹˜μ˜ μ½”λ“œμ™€ λΉ„μŠ·ν•œλ° λ‹€λ₯Έ 점이 μžˆλ‹€λ©΄ μ €λŠ” μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν–ˆλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€!

μ΅œμ†Œ μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•˜μ—¬ κ°€μž₯ λ¨Όμ € λλ‚˜λŠ” νšŒμ˜κ°€ top에 μ˜€λ„λ‘ ν•˜μ˜€μŠ΅λ‹ˆλ‹€. μ•½κ°„μ˜ νŠΈλ¦­μ„ μ‚¬μš©ν•΄ {endTime, startTime} κ³Ό 같이 큐에 λ„£μ–΄μ£Όμ–΄ compareν•˜λŠ” ν•¨μˆ˜λ₯Ό λ”°λ‘œ λ§Œλ“€μ§€ μ•Šμ•„λ„ λ˜λ„λ‘ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

즉,
pq.top().second -> μ‹œμž‘ μ‹œκ°„
pq.top().first -> λλ‚˜λŠ” μ‹œκ°„

// μ € λ¨Όμ € λλ‚˜λŠ” λ―ΈνŒ…μ„ μ§„ν–‰ν•œλ‹€.
int lastEnd = pq.top().first;
maxMeeting++;
pq.pop();

// 이후 λ¨Όμ € λλ‚˜λŠ” 회의순으둜 μˆœνšŒλœλ‹€
while (!pq.empty()) {
    int start = pq.top().second;
    int end = pq.top().first;
    pq.pop();
    // 이전 νšŒμ˜κ°€ λλ‚œ ν›„λ‚˜ 직후에 λ‹€μŒ νšŒμ˜κ°€ μ‹œμž‘λ˜μ–΄μ•Ό 함
    if (lastEnd <= start) {
        lastEnd = end;
        maxMeeting++;
    }
}

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.

μ–΄λ–€ 쑰건이 주어지고 μ΅œλŒ€λ₯Ό κ΅¬ν•˜λΌ λŠ” λ¬Έμ œλŠ” 주둜 κ·Έλ¦¬λ””λ¬Έμ œμž…λ‹ˆλ‹€.
λ”°λΌμ„œ 그리디 λ¬Έμ œμ— 맞게 μ ‘κ·Όν•˜μ˜€κ³ , μ–΄λŠμ‘°κ±΄μ—μ„œ μ΅œλŒ€μ˜ 회의수λ₯Ό ꡬ할 수 μžˆμ„μ§€ κ³ λ―Όν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.
여기에 μ“΄ μ‹œκ°„μ΄ ν‘Όμ‹œκ°„μ˜ λŒ€λΆ€λΆ„μ„ μ°¨μ§€ν•˜μ˜€μŠ΅λ‹ˆλ‹€..

슀슀둜 100% μƒμƒν•˜μ§„ λͺ»ν•˜μ˜€κ³ , 1ν•™κΈ° μ•Œκ³ λ¦¬μ¦˜ μˆ˜μ—…λ•Œ λ“€μ—ˆλ˜ λ‚΄μš©μ„ λ– μ˜¬λ €λ³΄λ©° 풀이법을 λ– μ˜¬λ¦¬κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

λ°”λ‘œ λ§ˆμΉ˜λŠ” μ‹œκ°„μˆœμœΌλ‘œ μ •λ ¬ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

즉 일찍마치면 μ•žμͺ½μ— λ°°μΉ˜ν•©λ‹ˆλ‹€.
첫번째회의λ₯Ό μ„ νƒν•˜κ²Œ 되면, 첫번쨰회의의 μ’…λ£Œμ‹œκ°μ΄ λ‘λ²ˆμ§ΈνšŒμ˜μ˜ μ‹œμž‘μ‹œκ°λ³΄λ‹€ μž‘κ±°λ‚˜ κ°™μ•„μ•Ό ν•©λ‹ˆλ‹€. 이λ₯Ό λ§Œμ‘±ν•˜λŠ” λ‘λ²ˆμ§ΈνšŒμ˜λ₯Ό μ„ νƒν•˜κ³  이λ₯Ό λ°˜λ³΅ν•©λ‹ˆλ‹€.

기쀀을 λλ‚˜λŠ” μ‹œκ°μœΌλ‘œ μž‘λŠ” μ΄μœ λŠ” μ΅œλŒ€ν•œ 많이 λ„£κΈ° μœ„ν•΄ μ΄μ „νšŒμ˜κ°€ λμ΄λ‚˜μ•Ό λ‹€μŒ 회의λ₯Ό μ‹œμž‘ν•  수 있기 λ•Œλ¬Έμž…λ‹ˆλ‹€.

import sys

input = sys.stdin.read
data = input().splitlines()

N = int(data[0])
meetings = []

for i in range(1, N+1):
    start, end = map(int, data[i].split())
    meetings.append((start, end))

meetings.sort(key=lambda x: (x[1], x[0]))

count = 0
end_time = 0

for start, end in meetings:
    if start >= end_time:
        count += 1
        end_time = end

print(count)
  • μΆ”κ°€λ‘œ μ•Œκ²Œλœ 점.
  1. νŒŒμ΄μ¬μ—μ„œ μ½”ν…Œλ¬Έμ œλ₯Ό ν’€λ•Œ μž…λ ₯을 input으둜 λ°›μ„λ•Œκ°€ 있고 input = sys.stdin.read둜 μ½μ„λ•Œκ°€ μžˆλŠ”λ° 이 기쀀을 λŒ€λž΅ N >= 10,000μΌλ•Œλ‘œ μž‘λŠ”κ²Œ μ’‹μŠ΅λ‹ˆλ‹€.

  2. μ—¬λŸ¬ κΈ°μ€€μœΌλ‘œ 정렬이 ν•„μš”ν• λ•Œ
    meetings.sort(key=lambda x: (x[1], x[0]))
    μ΄λŸ¬ν•œ 방식을 주둜 νŒŒμ΄μ¬μ—μ„œ μ‚¬μš©ν•©λ‹ˆλ‹€.

x[1]은 μ’…λ£Œμ‹œκ°„μ„ μš°μ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•œλ‹€λŠ” 뜻이고,
x[0]은 μ’…λ£Œμ‹œκ°„μ΄ κ°™μ„λ•Œ λ‘λ²ˆμ§Έ κΈ°μ€€μœΌλ‘œ μž‘λ™ν•©λ‹ˆλ‹€.

참고둜 lambdaλŠ” κ°„λ‹¨ν•œ λ‘œμ§μ„ μΌμ‹œμ μœΌλ‘œ μ •μ˜ν•΄μ•Όν• λ•Œ μ‚¬μš©ν•˜κ³ 
add = lambda x, y: x + y
print(add(2, 3)) # 좜λ ₯: 5
μ΄λŸ°μ‹μœΌλ‘œ μ‚¬μš©λ©λ‹ˆλ‹€.

@kokeunho kokeunho merged commit 0b21235 into main Jan 6, 2025
1 check passed
@g0rnn g0rnn deleted the 11-kokeunho branch January 6, 2025 17:08
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