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

9-wnsmir #34

Merged
merged 2 commits into from
Dec 10, 2024
Merged

9-wnsmir #34

merged 2 commits into from
Dec 10, 2024

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Nov 26, 2024

πŸ”— 문제 링크

이것이 취업을 μœ„ν•œ μ½”λ”©ν…ŒμŠ€νŠΈμ΄λ‹€ μ±…μ—μ„œ λ°œμ·Œν•œ 문제라 λ§ν¬λŠ” λ”°λ‘œ μ—†μŠ΅λ‹ˆλ‹€.

예λ₯Όλ“€μ–΄ 2 4 5 4 6으둜 이루어진 배열이 μžˆμ„λ•Œ M이 8이고 Kκ°€ 3이라면 K번만큼 κ°™μ€μˆ˜λ₯Ό μ—°μ†ν•΄μ„œ 더할 수 있고 총 더할 수 μžˆλŠ” μˆ˜λŠ” M번 즉 8λ²ˆμ΄λ‹€. M번 λ”ν–ˆμ„λ•Œ κ°€μž₯ 큰 수λ₯Ό κ΅¬ν•˜λΌ.

μž…λ ₯은 첫째쀄에 N, M, K κ°€ 주어지고 N은 λ°°μ—΄μ˜ 크기, M은 총 λ”ν•˜λŠ” 수 KλŠ” κ°™μ€μˆ˜κ°€ μ—°μ†μœΌλ‘œ λ”ν•΄μ§€λŠ” 수 이닀.
λ‘˜μ§Έμ€„μ— N개의 μˆ˜κ°€ 곡백으둜 κ΅¬λΆ„λ˜μ–΄ 주어진닀.

μž…λ ₯ μ˜ˆμ‹œ
5 8 3
2 4 5 4 6

좜λ ₯ μ˜ˆμ‹œ 46

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

20min

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

λŒ€ν‘œμ μΈ μ‰¬μš΄ 그리디 문제둜, μ—°μ†ν•΄μ„œ 올 수 μžˆλŠ” 수의 μ œν•œμ΄ μžˆλ‹€λŠ”κ±΄ κ·Έλ‹€μŒ μˆ˜λŠ” λ‘λ²ˆμ¨°λ‘œ 큰수λ₯Ό 더해주고 λ‹€μ‹œ μ—°μ†ν•΄μ„œ κ°€μž₯ 큰수λ₯Ό 더해주면 λœλ‹€. 즉 λ‚΄κ°€ ν•„μš”ν•œ μˆ«μžλŠ” κ°€μž₯ν°μˆ˜μ™€ λ‘λ²ˆμ§Έλ‘œ 큰수 λ‘κ°œλ§Œ ν•„μš”ν•œ 것이닀.

while문을 크게 돌며 Mλ²ˆμ„ μ±„μš°λ„λ‘ ν–ˆκ³ ,
for문을 돌며 k번 κ°€μž₯ 큰수λ₯Ό 더해주며

for문을 λ‚˜μ˜€λ©΄ Mλ²ˆμ„ λ„˜μ§€ μ•ŠλŠ” μ„ μ—μ„œ λ‘λ²ˆμ§Έλ‘œ 큰수λ₯Ό λ”ν•΄μ£ΌλŠ” μ‹μœΌλ‘œ μ½”λ“œλ₯Ό κ΅¬μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

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.

처음 문제λ₯Ό μ½κ³ λ‚˜μ„œλŠ”,
μ—°μ†ν•΄μ„œ K번 μ‚¬μš©ν•˜λ©΄ κ·Έ μˆ˜λŠ” λ²„λ €μ•Όν•˜λŠ” 쀄 μ•Œμ•˜λ„€μš©

문제λ₯Ό λ‹€μ‹œ μ½μ–΄λ³΄λ‹ˆκΉŒ μ€€μš©λ‹˜ μ„€λͺ…λŒ€λ‘œ κ°€μž₯ 큰 κ°’, λ‘λ²ˆμ§Έλ‘œ 큰 κ°’λ§Œ μ €μž₯ν•˜λ©΄ 되고
κ°€μž₯ 큰 값을 K번 λ”ν•˜κ³ , λ‘λ²ˆμ§Έλ‘œ 큰 값을 1번 λ”ν•˜κ³ , 이러면 λ˜κ² λ”λΌκ΅¬μš”

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

using namespace std;

int main() {
    int N, M, K;
    cin >> N >> M >> K;

    vector<int> input(N);
    for (int i = 0; i < N; i++) {
        cin >> input[i];
    }
    sort(input.begin(), input.end());
    int first = input[N - 1];
    int second = input[N - 2];

    int result = 0;
    for (int i = 0; i < M;) {
        for (int j = 0; j < K && i < M; j++, i++) {
            result += first;
        }
        if (i >= M) break;
        
        result += second;
        i++;
    }
    cout << result;

    return 0;
}

그리고 이거λ₯Ό μ΄λ ‡κ²Œ for문으둜 λŒλ¦¬μ§€ 말고 μˆ˜ν•™μ μœΌλ‘œ 미리 κ³„μ‚°ν•˜λ©΄
O(1) μ‹œκ°„λ³΅μž‘λ„λ₯Ό κ°€μ§€λŠ” μ½”λ“œλ„ μ™„μ„±ν•  수 μžˆμ„ λ“― ν•˜λ„€μš”!

μ•„λž˜ μ½”λ“œλŠ” 처음 ν‘Ό 방식에 λŒ€ν•œ μ½”λ“œμž…λ‹ˆλ .
ν‘Όκ²Œ μ•„κΉŒμ›Œμ„œ... λΌμ›ŒνŒ”κΈ° ν•˜κ² μŠ΅λ‹ˆλ‹€.

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

using namespace std;

int main() {
    int N, M, K;
    cin >> N >> M >> K;

    vector<int> input(N);
    for (int i = 0; i < N; i++) {
        cin >> input[i];
    }
    sort(input.begin(), input.end());

    int result = 0;
    int index = N-1;
    for (int i = 0; i < M;) {
        for (int j = 0; j < K; j++) {
            result += input[index];
            i++;
            if (i >= M) break;
        }
        index--;
    }
    cout << result;

    return 0;
}

@kangrae-jo kangrae-jo changed the title 9 wnsmir 9-wnsmir Dec 1, 2024
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.

숫자λ₯Ό λ‘κ°œ 밖에 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 점과 k번의 κ°€μž₯ 큰수 + 1번의 λ‘λ²ˆμ§Έ 큰 수의 ν•©μ˜ 연속이기 λ•Œλ¬Έμ— μˆ˜ν•™μ μœΌλ‘œ ν’€μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€. μž…λ ₯을 받을 λ•Œ κ°€μž₯ ν°μˆ˜μ™€ λ‘λ²ˆμ§Έλ‘œ 큰 수만 λ°›μ•˜μ–΄μš”

    int part = fir * k + sec;
    if(m <= k) cout<<fir*k;
    else {
        int cnt = m / (k+1);
        int rest = m % (k+1);
        cout << part * cnt + fir * rest;
    }

@wnsmir wnsmir merged commit 41b1744 into main Dec 10, 2024
1 check passed
@kangrae-jo kangrae-jo deleted the 9-wnsmir branch December 10, 2024 15:38
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.

3 participants