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

8-rivkms #28

Merged
merged 2 commits into from
Mar 11, 2024
Merged

8-rivkms #28

merged 2 commits into from
Mar 11, 2024

Conversation

rivkms
Copy link
Collaborator

@rivkms rivkms commented Mar 4, 2024

πŸ”— 문제 링크

νšŒμ˜μ‹€ λ°°μ •

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

30λΆ„

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

βœ”οΈ 문제 μœ ν˜•

: Greedy

GreedyλŠ” 사싀 μ œκ°€ λ³„λ‘œ μ•ˆμ’‹μ•„ν•˜κΈ΄ ν•©λ‹ˆλ‹€.(사싀 μ € 말고 제 μ§€λ„κ΅μˆ˜λ‹˜μ΄ μ‹«μ–΄ν•˜μ…¨μ–΄μš”.) κ·Έ μ΄μœ λŠ” μ œν•œμ μΈ μƒν™©μ—μ„œλ§Œ μ‚¬μš©λ˜κΈ°μ— λ¬Έμ œμ—μ„œ 주어진 상황이 Greedy둜 ν’€ 수 μžˆλŠ” μƒν™©μž„μ„ 증λͺ…ν•΄μ•Όν•œλ‹€κ³  κ°•μ‘°"λ‹Ήν–ˆκΈ°" λ•Œλ¬Έμ΄μ£ .

λ¬Όλ‘  λ§žλŠ” λ§μž…λ‹ˆλ‹€. κ·Έλž˜μ„œ Greedyλ₯Ό μ“°λ©΄ λ°˜λ‘€κ°€ μžˆμ–΄ Greedy둜 ν’€μ΄ν•˜λ©΄ ν‹€λ¦¬λŠ” λ¬Έμ œκ°€ μžˆκΈ°λ„ ν•˜μ£ .
일단 이번 λ¬Έμ œλŠ” 정말 μ‰½κ²Œ Greedy둜 풀이할 수 μžˆλŠ” μ‰¬μš΄ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€.

Algorithm

μ‚¬μš©ν•  수 μžˆλŠ” μ•Œκ³ λ¦¬μ¦˜μ—λŠ” λŒ€μΆ© 4가지가 μžˆλŠ” κ²ƒμœΌλ‘œ μ••λ‹ˆλ‹€.

1️⃣ 회의 진행 μ‹œκ°„μ΄ 짧은 것뢀터 λ°°μ •ν•˜κΈ°
2️⃣ νšŒμ˜κ°€ λ¨Όμ € μ§„ν–‰λ˜λŠ” 것뢀터 λ°°μ •ν•˜κΈ°
3️⃣ νšŒμ˜κ°€ λ¨Όμ € λλ‚˜λŠ” 것뢀터 λ°°μ •ν•˜κΈ°
4️⃣ 회의 진행 μ‹œκ°„μ΄ κΈ΄ 것뢀터 λ°°μ •ν•˜κΈ°

이 μ€‘μ—μ„œ κ°€μž₯ 효과적인 방법은, νšŒμ˜κ°€ λ¨Όμ € λλ‚˜λŠ” 것뢀터 λ°°μ •ν•˜κΈ° μž…λ‹ˆλ‹€.
μžμ„Έν•œ μ΄μœ μ— λŒ€ν•˜μ—¬λŠ” λ‹€μŒ 글을 ν•œλ²ˆ μ°Έκ³ ν•΄μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² κ³ μš”,
image

μš”μ•½ν•˜μžλ©΄, μ’…λ£Œ μ‹œκ°„μ΄ λŠ¦λŠ” 회의일 경우 λ‹€μŒλ²ˆμ— 올 수 μžˆλŠ” μ‹œκ°„ 및 νšŒμ˜κ°€ 더 μ μ–΄μ§ˆ 수 μžˆκΈ°μ— νšŒμ˜κ°€ λ¨Όμ € λλ‚˜λŠ” 것이 더 μ’‹λ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€!!

sudo code

이λ₯Ό λ°”νƒ•μœΌλ‘œ sudo codeλ₯Ό μž‘μ„±ν•΄λ³΄μžλ©΄,

1️⃣ μž…λ ₯을 λ°›λŠ”λ‹€. 
2️⃣ νšŒμ˜κ°€ λ¨Όμ € λλ‚˜λŠ” 순으둜 μ •λ ¬ν•œλ‹€. 
    2️⃣ -1 νšŒμ˜κ°€ λλ‚˜λŠ” μ‹œκ°„μ΄ κ°™λ‹€λ©΄, λ¨Όμ € μ‹œμž‘ν•˜λŠ” 순으둜 μ •λ ¬ν•œλ‹€. 
3️⃣ 맨 처음 회의λ₯Ό tmp에 μ €μž₯ν•œλ‹€. 
4️⃣ λ°˜λ³΅λ¬Έμ„ μ‚¬μš©ν•˜μ—¬ λͺ¨λ“  κ²½μš°μ— λŒ€ν•˜μ—¬ λ‹€μŒμ˜ 연산을 μˆ˜ν–‰ν•œλ‹€. 
    4️⃣ -1 tmp에 μ €μž₯된 회의의 λλ‚˜λŠ” μ‹œκ°„λ³΄λ‹€ μ§€κΈˆμ˜ 회의의 μ‹œμž‘μ‹œκ°„μ΄ λΉ λ₯΄λ©΄ λ„˜μ–΄κ°„λ‹€. 
    4️⃣ -2 κ·Έ μ™Έμ˜ 경우 tmp에 μ§€κΈˆμ˜ 회의λ₯Ό μ €μž₯ν•˜κ³  countλ₯Ό μ¦κ°€ν•œλ‹€. 

πŸ–₯️ μ½”λ“œ

#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>> v(n);
    for(int i = 0; i<n; i++){
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end(), compare);
    pair<int, int> tmp = v.front();
    int s = 1;
    for(int i = 1; i < n; i++){
        if(tmp.second <= v[i].first){
            tmp = v[i];
            s++;
        }
    }
    cout << s;

    return 0;
}

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

βœ”οΈ Greedy μ•Œκ³ λ¦¬μ¦˜μ€ 잘λͺ»μ“°λ©΄ ν°μΌλ‚œλ‹€... λͺ¨λ“  κ²½μš°μ— λŒ€ν•˜μ—¬ Greedyκ°€ 효과적인 것은 μ•„λ‹ˆλ‹€.

βœ”οΈ μž‘μ—… ν• λ‹Ήκ³Ό κ΄€λ ¨λœ 문제λ₯Ό ν’€ 수 μžˆλŠ” λ‹€μ–‘ν•œ μ•Œκ³ λ¦¬μ¦˜μ΄ μžˆμ§€λ§Œ, κ²½μš°μ— 따라 κ°€μž₯ 효율적인 μ•Œκ³ λ¦¬μ¦˜μ΄ μžˆλ‹€.

Copy link
Member

@kjs254 kjs254 left a comment

Choose a reason for hiding this comment

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

νšŒμ˜κ°€ λ¨Όμ € λλ‚˜λŠ” 것을 μ„ νƒν•˜λŠ” 것에 λŒ€ν•œ 이해가 잘 λ˜μ§€ μ•Šμ•˜λŠ”λ° μ˜¬λ €μ£Όμ‹  λΈ”λ‘œκ·Έλ₯Ό μ½μ–΄λ³΄λ‹ˆ μ‰½κ²Œ 이해가 λ©λ‹ˆλ‹€.

졜적의 경우만 κ³ λ €ν•˜λŠ” μ „ν˜•μ μΈ 그리디 문제둜 틀림이 μ—†λ„€μš”.

μ œκ°€ 그리디 문제λ₯Ό ν’€ λ•ŒλŠ” 늘 μ‹œκ°„ μ΄ˆκ³Όκ°€ λ–΄λŠ”λ° 정닡을 μ•Œκ³  ν’€λ©΄ 늘 μ‰¬μš΄ λ¬Έμ œκ°€ 그리디 문제라고 μƒκ°ν•©λ‹ˆλ‹€.

μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€!

Copy link
Collaborator

@mong3125 mong3125 left a comment

Choose a reason for hiding this comment

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

μ§κ΄€μ μœΌλ‘œ 이해가 ν•œλ°©μ— λ˜λ„€μš”. 잘 λ΄€μŠ΅λ‹ˆλ‹€.

tmi. μ „ 30λΆ„λ§Œμ— λͺ»ν’€μ–΄μ„œ 정닡을 λ΄€μŠ΅λ‹ˆλ‹€. 정렬을 ν•˜κ³  κ·Έλ¦¬λ””λ‘œ ν’€κ±°λ‚˜ DFS둜 ν’€μ–΄μ•Όκ² λ‹€λŠ” 생각은 λ“€μ—ˆλŠ”λ° λ§ˆμΉ˜λŠ” μ‹œκ°„μ— μ§‘μ€‘ν•˜μ§ˆ λͺ»ν–ˆλ„€μš”..

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

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

μ§κ΄€μ μœΌλ‘œ 이해가 λ”± λ˜λ„€μš”.
μ½”λ“œ κ΅¬ν˜„λ„ ꡰ더더기 없이 κΉ”λ”ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€. μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€!

@rivkms rivkms merged commit 0f87622 into main Mar 11, 2024
@rivkms rivkms deleted the 8-rivkms branch March 11, 2024 08:49
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