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

35-pknujsp #137

Merged
merged 4 commits into from
Mar 3, 2024
Merged

35-pknujsp #137

merged 4 commits into from
Mar 3, 2024

Conversation

pknujsp
Copy link
Collaborator

@pknujsp pknujsp commented Feb 14, 2024

πŸ”— 문제 링크

선물할인

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

였랜 μ‹œκ°„

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

N개의 선물이 μžˆμ„ λ•Œ, μ΅œλŒ€ A 개의 선물에 λŒ€ν•΄μ„œ λ°˜κ°’ 할인을 μ μš©ν•˜μ—¬ 주어진 μ˜ˆμ‚° B둜 ꡬ맀가λŠ₯ν•œ μ„ λ¬Όμ˜ μ΅œλŒ€ 개수λ₯Ό κ΅¬ν•˜λŠ” 문제

  • 졜근 ν‘Ό 문제 쀑 체감 λ‚œμ΄λ„κ°€ μ΅œμƒ
    • κ·Έλ¦¬λ””λ‘œ νŒŒμ•…μ€ λΉ λ₯΄κ²Œ ν–ˆμ§€λ§Œ
    • μ˜ˆμ‚°, μ΅œλŒ€ 할인 개수 의 크기λ₯Ό μ œλŒ€λ‘œ ν™•μΈν•˜μ§€ μ•Šμ•˜κ³ , 각각 0이 ν—ˆμš©λ˜λŠ”λ°, 이λ₯Ό λͺ¨λ₯΄λŠ” μƒνƒœμ—μ„œ 문제λ₯Ό ν’ˆ
    • 문제λ₯Ό 잘 μ½μ—ˆμ–΄μ•Ό ν•˜λŠ”λ° κΈ‰ν•˜κ²Œ ν’€λ €κ³  ν•˜λ‹€λ³΄λ‹ˆ 였히렀 μ‹œκ°„μ„ 더 많이 λ‚­λΉ„ν–ˆκ³ , λ‹€μ‹œ λ°˜μ„±ν•˜λŠ” 계기가 됨

풀이

  • μ„ λ¬Ό 가격을 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ :
    • min_price_gift : κ΅¬λ§€ν•œ μ„ λ¬Ό 쀑 μ •κ°€κ°€ κ°€μž₯ μ €λ ΄ν•œ 것
    • max_price_gift : κ°€μž₯ μ΅œκ·Όμ— κ΅¬λ§€ν•œ 것
  • μ΅œλŒ€ 할인 개수 만큼 μ €λ ΄ν•œ 것 λΆ€ν„° 할인받아 ꡬ맀
    • 쀑간에 μ˜ˆμ‚°μ΄ λ™λ‚˜λ©΄ λ§ˆμ§€λ§‰ μ„ λ¬Όμ˜ 인덱슀λ₯Ό 좜λ ₯ν•œ ν›„ μ’…λ£Œ
    • max_price_gift++ : μƒˆλ‘œ μ‚΄λ•Œ λ§ˆλ‹€ μ–΄λ–€ 선물을 μƒ€λŠ”μ§€ 기둝
  • max_price_gift < N 을 쑰건으둜 반볡
    • λͺ¨λ“  선물을 샀을 λ•Œ 루프 μ’…λ£Œ
    • λ°˜λ³΅ν•  λ•Œ λ§ˆλ‹€ 할인을 받을 수 μžˆλŠ”μ§€ 확인
    • μ΅œλŒ€ 할인 κ°€λŠ₯ κ°œμˆ˜κ°€ 0 이면, 할인 없이 μ •κ°€λ‘œ ꡬ맀
    • 할인 κ°€λŠ₯ : ν˜„μž¬ μ„ λ¬Ό(max_price_gift) 할인받아 ꡬ맀, μ˜ˆμ‚° λ™λ‚˜λ©΄ μ’…λ£Œ
    • 할인 λΆˆκ°€ : ν˜„μž¬ 계산에 ν¬ν•¨λœ μ„ λ¬Ό 쀑 μ •κ°€κ°€ κ°€μž₯ μ €λ ΄ν•œ min_price_gift λ₯Ό μ •κ°€λ‘œ λ³€κ²½ν•΄ ꡬ맀
  • max_price_gift 좜λ ₯
    • ꡬ맀가λŠ₯ν•œ μ„ λ¬Όμ˜ 개수
n, budget, max_sales = map(int, stdin.readline().split())
gifts = sorted(list(map(int, stdin.readline().split())))

min_price_gift = max_price_gift = 0
prices = 0
sales = 0

# μš°μ„  할인 κ°€λŠ₯ν•œ 개수만큼 λͺ¨λ‘ 할인받아 ꡬ맀
for sale_gift in range(max_sales):
    prices += gifts[sale_gift] // 2
    max_price_gift += 1
    
    # 사닀가 μ˜ˆμ‚° λ‹€ 떨어지면 μ’…λ£Œ
    if prices > budget:
        print(sale_gift)
        exit()

# ν˜„μž¬κΉŒμ§€ 할인받은 μˆ˜λŸ‰
sales = max_price_gift - min_price_gift

# λͺ¨λ“  선물을 샀을 λ•Œ μ’…λ£Œ
while max_price_gift < n:
    if sales < max_sales or max_sales == 0:

        # 할인 κ°€λŠ₯ν•œ κ°œμˆ˜κ°€ 0이면 μ •κ°€λ‘œ μ‚°λ‹€
        if max_sales == 0:
            prices += gifts[max_price_gift]
        else:
            prices += gifts[max_price_gift] // 2
        
        # μ˜ˆμ‚° λ‹€ 떨어지면 μ’…λ£Œ
        if prices > budget:
            break

        max_price_gift += 1
        sales += 1
    else:
        # 할인이 λΆˆκ°€λŠ₯ν•˜λ―€λ‘œ μ •κ°€κ°€ κ°€μž₯ μ €λ ΄ν•œ 선물을 할인없이 μ •κ°€λ‘œ μ‚°λ‹€
        prices += gifts[min_price_gift] // 2
        min_price_gift += 1
        sales -= 1

print(max_price_gift)

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

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

present_count, money, original_sale_count = map(int,input().split())
present_prices = sorted(list(map(int,input().split())))

stack = []
total_price = 0
sale_idx = 0
sale_count = original_sale_count

for price in present_prices:
    if original_sale_count > 0:
        if sale_count > 0 and money < (total_price + price//2): break
        
        if sale_count > 0 and money >= (total_price + price//2):
           stack.append(price//2)
            total_price += price//2
            sale_count -= 1

        elif money >= (total_price + stack[sale_idx] + price //2):
            stack.append(price//2)
            total_price += price//2
            total_price += stack[sale_idx]
            stack[sale_idx] *= 2
            sale_idx += 1

        else: break

    else:
        if money >= total_price + price:
            stack.append(price)
            total_price += price

        else: break

print(len(stack))

와 μ „ 이 λ¬Έμ œκ°€ 그리디 인 쀄 μ „ν˜€ λͺ°λžμ–΄μš”.

κ·Έλƒ₯ λ‹¨μˆœ μŠ€νƒμΈμ€„ μ•Œμ•˜λ„€μš”.

μ €λŠ” 일단 할인할 수 μžˆλŠ” 물건 만큼 ν• μΈλœ κ°€κ²©μœΌλ‘œ 사고,

이제 할인 κ°€λŠ₯ν•œ κ°œμˆ˜κ°€ 끝났을 경우 사렀고 ν•˜λŠ” 물건을 a라고 ν–ˆμ„ λ•Œ

κ°€μž₯ μ‹Έκ²Œ ν• μΈν•œ 물건 λΆ€ν„° μ •κ°€λ‘œ 올린 λ’€, a의 물건을 λ°˜κ°’μœΌλ‘œ ν• μΈν•΄μ„œ μ‚΄ 수 μžˆλ‚˜? λ₯Ό μ²΄ν¬ν•˜λŠ” μ‹μœΌλ‘œ μŠ€νƒμœΌλ‘œ μ €μž₯ν•΄λ‚˜κ°”μ–΄μš”.

λ­”κ°€ ν’€μ΄λŠ” λΉ„μŠ·ν•œ 것 같은데..

μ €λŠ” κ·Έλƒ₯ λ‹¨μˆœ μŠ€νƒμ΄λΌκ³  μƒκ°ν•΄μ„œ ν’€μ–΄μ„œ κ·ΈλŸ°μ§€ 비ꡐ적 μ‰½κ²Œ 느껴쑌던 것 κ°™λ„€μš”.

@pknujsp
Copy link
Collaborator Author

pknujsp commented Feb 18, 2024

μ§„μ§œ μ‰½κ²Œ ν‘Έμ…¨λ„€μš”

μ „ λ¬Έμ œμ—μ„œ μ œν•œλœ 돈으둜 μ΅œλŒ€ν•œ 많이 사봐라고 ν•΄μ„œ 그리디가 λ– μ˜¬λžμ–΄μš”
λ‹€μ‹œ κ³ λ―Όν•΄λ³΄λ‹ˆ νƒœκ·œλ‹˜ μ–˜κΈ°μ²˜λŸΌ κ°„λ‹¨ν•˜κ²Œ μˆœνšŒν•˜λ©΄μ„œ νλ‚˜ μŠ€νƒμœΌλ‘œ ν’€ 수 μžˆλŠ” 문제인거 κ°™λ„€μš”

둜직 λ§ν•˜μ‹  것 처럼 λ„“κ²Œ 봀을 λ•Œ λΉ„μŠ·ν•œκ±° κ°™μ•„μš”

제 μ½”λ“œλŠ” 처음 할인받을 λ•Œ μ „ κ°€λŠ₯ν•œ 개수만큼 λ”°λ‘œ 할인받아보고, 더 이상 할인 λͺ»λ°›μ„ λ•Œ λ‹€μŒ μ„ λ¬Όλ‘œ λ„˜μ–΄κ°€μ„œ
제일 μ‹Ό κ±°λŠ” μ •κ°€λ‘œ λ°”κΎΈκ³ , μƒˆλ‘œ λ§Œλ‚˜λŠ” 선물은 할인 λ°›μ•„κ°€λ©΄μ„œ 총 할인 개수λ₯Ό μœ μ§€ν•΄κ°€λŠ” 방식인데,

νƒœκ·œλ‹˜μ€ 선물을 ν•˜λ‚˜μ”© ν™•μΈν•˜λ©΄μ„œ λ‹€ μ²˜λ¦¬ν•˜λŠ” λ°©μ‹μ΄κ΅°μš”

특히 κ΅¬λ§€λŸ‰μ„ 관리할 λ•Œκ°€ 차이가 μžˆλ„€μš”

μ €λŠ” ν•œμ°Έ 헀맀닀 λ³΄λ‹ˆ 할인 개수λ₯Ό 쒌우 인덱슀둜 κ΄€λ¦¬ν•˜λŠ”κ²Œ λ– μ˜¬λΌμ„œ 인덱슀둜 ν–ˆλŠ”λ°
이 뢀뢄은 리슀트둜 λ‹€λ£¨μ…¨κ΅°μš”

@yuna83
Copy link
Member

yuna83 commented Feb 25, 2024

"선물가격을 μ˜€λ¦„μ°¨μˆœ μ •λ ¬ν•΄μ„œ λ°˜κ°’ν• μΈμ΄ κ°€λŠ₯ν•œ 수만큼 할인을 ν•΄μ„œ ꡬ맀λ₯Ό ν•˜κ³ 
λ°˜κ°’ν• μΈμ΄ κ°€λŠ₯ν•œ μˆ˜κ°€ 0이 되면 κ°€μž₯ 가격이 μž‘μ€ 선물을 μ •κ°€λ‘œ κ΅¬λ§€ν•˜λŠ” 걸둜 λ³€κ²½, ν˜„μž¬ 선물을 ν• μΈν•΄μ„œ ꡬ맀.
이 과정을 μ˜ˆμ‚°μ„ μ΄ˆκ³Όν•  λ•ŒκΉŒμ§€ μ‹€ν–‰"

이 풀이흐름을 μ΄ν•΄ν•˜λŠ”λ° μ‹œκ°„μ„ λ‹€ μΌλ„€μš”
μ²˜μŒμ—” κ·Έλ¦¬λ””λ‘œ ν’€λ©΄ λ˜κ² λ‹€ μ‹Άμ–΄μ„œ ν’€ 수 μžˆμ„ 쀄 μ•Œμ•˜λŠ”λ°
a(λ°˜κ°’ν• μΈ κ°€λŠ₯ν•œ μ„ λ¬Όμ˜ 수)λ•Œλ¬Έμ— λ§‰νžˆλ„€μš”
λ‹€λ₯Έ 풀이듀도 많이 μ°Ύμ•„λ΄μ•Όκ² μ–΄μš”!!

@Munbin-Lee
Copy link
Member

Code
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, b, a;
    cin >> n >> b >> a;

    vector<int> gifts(n);

    for (int &gift: gifts) {
        cin >> gift;
    }

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

    int answer = 0;
    int cost = 0;

    while (true) {
        if (answer >= a) {
            cost += gifts[answer - a] / 2;
        }

        cost += gifts[answer] / 2;

        if (cost > b) break;

        answer++;

        if (answer == n) break;
    }

    cout << answer;

    return 0;
}

μ–΄λ–»κ²Œ κΉ”λ”ν•˜κ²Œ κ΅¬ν˜„ν• μ§€ κ³ λ―Όν•˜λ‹€ ν˜„μž¬ μœ„μΉ˜ - a 의 절반 값을 λ”ν•˜λ©΄ μƒμ‡„λœλ‹€λŠ” κ±Έ κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€.

@pknujsp pknujsp merged commit 64cd3b5 into main Mar 3, 2024
1 check passed
@pknujsp pknujsp deleted the 35-pknujsp branch March 3, 2024 07:36
@pknujsp pknujsp restored the 35-pknujsp branch March 3, 2024 07:39
@pknujsp pknujsp mentioned this pull request Mar 3, 2024
@pknujsp pknujsp deleted the 35-pknujsp branch March 3, 2024 08:40
@pknujsp pknujsp restored the 35-pknujsp branch March 3, 2024 08:40
@tgyuuAn tgyuuAn deleted the 35-pknujsp branch May 10, 2024 13:30
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