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

29-Dolchae #109

Merged
merged 3 commits into from
Feb 22, 2024
Merged

29-Dolchae #109

merged 3 commits into from
Feb 22, 2024

Conversation

Dolchae
Copy link
Collaborator

@Dolchae Dolchae commented Feb 19, 2024

πŸ”— 문제 링크

2961 λ„μ˜μ΄κ°€ λ§Œλ“  λ§›μžˆλŠ” μŒμ‹

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

50λΆ„?

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

문제

재료의 κ°œμˆ˜μ™€ κ·Έ 재료의 신맛과 쓴맛이 주어지면 신맛과 μ“΄λ§›μ˜ 차이가 κ°€μž₯ μž‘μ€ μš”λ¦¬μ˜ 차이λ₯Ό 좜λ ₯ν•˜λŠ” λ¬Έμ œμ΄λ‹€.
μ΄λ•Œ, κ·Έ μŒμ‹μ˜ 신맛은 μ‚¬μš©ν•œ 재료의 μ‹ λ§›μ˜ 곱이고, μ“΄λ§›μ˜ 합이닀.

μ½”λ“œ

import sys
from itertools import combinations
input = sys.stdin.readline

n = int(input())

ingrid = [[0 for _ in range(2)] for _ in range(n)]
for i in range(n):
    ingrid[i][0],ingrid[i][1] = map(int,input().split())
  • 쑰합을 μ‚¬μš©ν•˜κΈ° μœ„ν•΄ combinationsλ₯Ό import ν•΄μ€€λ‹€.
  • 재료의 개수 nκ³Ό 재료의 신맛과 쓴맛을 담은 리슀트 ingridλ₯Ό μ €μž₯ν•œλ‹€.
    • ingrid[.][0]μ—λŠ” 신맛이, ingrid[.][1]μ—λŠ” 쓴맛을 μ €μž₯ν•œλ‹€.

ans = abs(ingrid[0][0] - ingrid[0][1])
for i in range(1,n+1): #1
    for comb in combinations(ingrid,i): #2
        print(comb)
        sour, bitter = 1,0
        for j in range(i): #3
            sour *= comb[j][0]
            bitter += comb[j][1]
        now_diff = abs(sour-bitter)
        if now_diff < ans: 
            ans = now_diff

print(ans)
  • λ§ˆμ§€λ§‰μ— 좜λ ₯ν•  μ΅œμ†Œ 차이인 ansλ₯Ό 맨 처음 재료 ν•˜λ‚˜λ§Œ 썼을 λ•Œμ˜ 경우둜 μ •μ˜ν•΄μ€€λ‹€.
  • for문에 λ§Žμ•„μ„œ μ’€ (많이) λ³΅μž‘ν•΄ λ³΄μ΄μ§€λ§Œ μ΄λ ‡κ²Œ 정리할 수 μžˆλ‹€.

#1

  • 재료 κ°œμˆ˜μ— 따라 κ°€λŠ₯ν•œ λͺ¨λ“  쑰합을 μƒμ„±ν•˜κΈ° μœ„ν•œ for문이닀.

#2

  • combinations ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄ ν˜„μž¬ 재료 κ°œμˆ˜μ— λŒ€ν•œ λͺ¨λ“  쑰합을 μƒμ„±ν•œλ‹€.
  • combinations(ingrid,i)λŠ” ingrid λ¦¬μŠ€νŠΈμ—μ„œ i개의 μš”μ†Œλ‘œ λ§Œλ“€ 수 μžˆλŠ” λͺ¨λ“  쑰합을 λ°˜ν™˜ν•˜κ³  이λ₯Ό comb에 μ €μž₯ν•œλ‹€.
    • μ—¬κΈ°μ„œ λ°˜ν™˜λœ 쑰합은 i개의 μš”μ†Œλ₯Ό 가지고, 각 μš”μ†ŒλŠ” [신맛,쓴맛] 이런 ν˜•νƒœλ₯Ό 가진 λ¦¬μŠ€νŠΈμ΄λ‹€.

#3

  • comb의 길이가 iμ΄λ―€λ‘œ i만큼 for문을 λ°˜λ³΅ν•œλ‹€.
  • sour와 bitterλ₯Ό 각각 κ³±ν•˜κ³  더해 μ €μž₯ν•˜κ³ , now_diff에 이 차이λ₯Ό μ €μž₯ν•œλ‹€.
  • λ§Œμ•½ ansκ°€ now_diff보닀 크닀면 ansκ°€ μ΅œμ†Œκ°€ μ•„λ‹ˆλΌλŠ” λœ»μ΄λ―€λ‘œ ansλ₯Ό κ°±μ‹ ν•΄μ€€λ‹€.
    λ§ˆμ§€λ§‰μœΌλ‘œ ansλ₯Ό 좜λ ₯ν•΄μ€€λ‹€.


전체 μ½”λ“œ

import sys
from itertools import combinations
input = sys.stdin.readline

n = int(input())

ingrid = [[0 for _ in range(2)] for _ in range(n)]
for i in range(n):
    ingrid[i][0],ingrid[i][1] = map(int,input().split())

ans = abs(ingrid[0][0] - ingrid[0][1])
for i in range(1,n+1): #1
    for comb in combinations(ingrid,i): #2
        print(comb)
        sour, bitter = 1,0
        for j in range(i): #3
            sour *= comb[j][0]
            bitter += comb[j][1]
        now_diff = abs(sour-bitter)
        if now_diff < ans: 
            ans = now_diff

print(ans)

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

combinations ν•¨μˆ˜μ— 리슀트λ₯Ό 주어도 μ•Œμ•„μ„œ λ‹€ μ²˜λ¦¬ν•΄μ€€λ‹€λŠ” 점이 정말 κ°λ™μŠ€λŸ¬μ› λ‹€. combinations(+permutations) ν•¨μˆ˜λŠ” λ‹€μŒ λΈ”λ‘œκ·Έμ—μ„œ κ³΅λΆ€ν–ˆλ‹€!


μ‘°ν•© 곡뢀 λΈ”λ‘œκ·Έ

@Dolchae Dolchae removed the request for review from gjsk132 February 20, 2024 10:21
Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

λ¬Έμ œν‘ΈλŠ”λ° 쑰합을 μ–΄λ–»κ²Œ κ΅¬ν˜„ν• μ§€ 정말 λ§‰λ§‰ν•˜κ³  μ–΄λ €μ› λŠ”λ°...!!πŸ™ƒ
combinations() ν•¨μˆ˜ μ“°κ³  κ°λ™μ˜ 눈물 ν˜λ¦΄λ»” ν–ˆμŠ΅λ‹ˆλ‹€.
μ•žμœΌλ‘œλ„ μœ μš©ν•˜κ²Œ μ“Έ 수 μžˆμ„ 것 κ°™μ•„μš”!
잘 μ •λ¦¬λœ λΈ”λ‘œκ·Έ κ³΅μœ ν•΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€.πŸ‘

Comment on lines +16 to +21
for j in range(i): #3
sour *= comb[j][0]
bitter += comb[j][1]
now_diff = abs(sour-bitter)
if now_diff < ans:
ans = now_diff
Copy link
Member

Choose a reason for hiding this comment

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

        for k in j:
            sour *= k[0]
            bitter += k[1]
        ans = min(ans, abs(sour-bitter))

제 μ½”λ“œμ™€ λΉ„κ΅ν•΄λ³΄λ‹ˆ μœ„μ˜ λΆ€λΆ„λ§κ³ λŠ” λΉ„μŠ·ν•˜λ„€μš”.πŸ™‚
수고 λ§ŽμœΌμ…¨μŠ΅λ‹ˆλ‹€!

Copy link
Member

Choose a reason for hiding this comment

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

μΆ”κ°€μ μœΌλ‘œ 덧뢙이면, λ¬Έμ œμ—μ„œ 신맛과 쓴맛 λͺ¨λ‘ 1,000,000,000보닀 μž‘μ€ μ–‘μ˜ μ–‘μˆ˜λΌκ³  ν•˜μ˜€μœΌλ―€λ‘œ ans = 1000000000도 μΆ”κ°€μ μœΌλ‘œ 써주어 min으둜 κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

μ‘°ν•© 아이디어 λ”± λ– μ˜¬λ €μ„œ μ μš©ν•˜λŠ” μ„ΌμŠ€ μ•„μ£Ό μ’‹μ•˜μŠ΅λ‹ˆλ‹€.
λ‹€μŒλ²ˆμ—”... 직접 κ΅¬ν˜„ν•΄λ³΄κΈ°? πŸ€ͺ

Comment on lines +7 to +9
ingrid = [[0 for _ in range(2)] for _ in range(n)]
for i in range(n):
ingrid[i][0],ingrid[i][1] = map(int,input().split())
Copy link
Collaborator

Choose a reason for hiding this comment

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

ingridients = [list(map(int, input().split())) for _ in range(N)]

μ§œμž” 리슀트 μ»΄ν”„λ¦¬ν—¨μ…˜μ„ μ‚¬μš©ν•˜λ©΄ ν•œ μ€„λ‘œ λœλ‹΅λ‹ˆλ‹€


ans = abs(ingrid[0][0] - ingrid[0][1])
for i in range(1,n+1): #1
for comb in combinations(ingrid,i): #2
Copy link
Collaborator

Choose a reason for hiding this comment

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

μ•„ 파이썬 itertools λΌμ΄λΈŒλŸ¬λ¦¬λŠ” λͺ‡ λ²ˆμ„ 봐도 λΆ€λŸ½λ„€μš”...

ν•˜μ§€λ§Œ μˆœμ—΄, 쑰합은 직접 μ†μœΌλ‘œ μ§œλ³΄λŠ” κ±Έ κ°•λ ₯ ꢌμž₯ν•©λ‹ˆλ‹€. λ°±νŠΈλž˜ν‚Ήμ˜ κ°œλ…μ„ μ΅νžˆλŠ” 데 λ„ˆλ¬΄λ„ˆλ¬΄ μ’‹κ±°λ“ μš”.

λ‹€μŒ λ²ˆμ—” 직접 κ΅¬ν˜„ν•΄λ΄μš”! Nκ³Ό M μ‹œλ¦¬μ¦ˆ μš”κ²Œ μ—°μŠ΅ν•˜κΈ° μ°Έ μ’‹μŠ΅λ‹ˆλ‹€.

Comment on lines +16 to +18
for j in range(i): #3
sour *= comb[j][0]
bitter += comb[j][1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

for s, b in comb:
    sour *= s
    bitter += b

μš”λ ‡κ²Œ λ°”λ‘œ κΊΌλ‚΄ 써도 μ’‹κ² μ£ ?

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