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

39-H0ngJu #266

Merged
merged 1 commit into from
Feb 3, 2025
Merged

39-H0ngJu #266

merged 1 commit into from
Feb 3, 2025

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Jan 18, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์ปฌ๋Ÿฌ๋ณผ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2H

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

image

๊ณจ2 ํ˜ธ๊ธฐ๋กญ๊ฒŒ ๊ฑด๋“œ๋ ธ๋‹ค๊ฐ€ ๋งž์™œํ‹€ ์ง€์˜ฅ์— ๊ฐ‡ํ˜”์Šต๋‹ˆ๋‹ค..




์ผ๋‹จ ๋ฌธ์ œ๋ฅผ ๋ณด์‹œ๋ฉด,

  1. ์™„ํƒ์œผ๋กœ๋Š” ์ ‘๊ทผ์ด ์–ด๋ ต๋‹ค.
  2. ์ด๊ฑฐ ์ •๋ ฌ ๋•Œ๋ฆฌ๋ฉด ๋ˆ„์ ํ•ฉ์œผ๋กœ ํ’€๋ฆฌ๊ฒ ๋Š”๋ฐ?
    ๊นŒ์ง€๋Š” ๋– ์˜ฌ๋ฆฌ์‹ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.



๋‹ค๋งŒ,,,

  1. ์ด์ „ ๊ณต๊ณผ ํฌ๊ธฐ๋Š” ๊ฐ™์ง€๋งŒ ์ƒ‰๊น”์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ
  2. ์ด์ „ ๊ณต๊ณผ ํฌ๊ธฐ๋Š” ๋‹ค๋ฅด์ง€๋งŒ ์ƒ‰๊น”์ด ๊ฐ™์€ ๊ฒฝ์šฐ
  3. ์ด์ „ ๊ณต๊ณผ ํฌ๊ธฐ์™€ ์ƒ‰๊น”์ด ๋ชจ๋‘ ๊ฐ™์€ ๊ฒฝ์šฐ
  4. ์ด์ „ ๊ณต๊ณผ ํฌ๊ธฐ์™€ ์ƒ‰๊น”์ด ๋ชจ๋‘ ๋‹ค๋ฅธ ๊ฒฝ์šฐ

์˜ ๋กœ์ง์„ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค.




์ œ ์ƒ๊ฐ์—๋Š”,
์ •๋ ฌ์„ ํ–ˆ๋ƒ ์•ˆํ–ˆ๋ƒ, ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•˜์˜€๋Š”๊ฐ€ ์— ๋”ฐ๋ผ 4๊ฐ€์ง€ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹ค์†Œ ๋‹ค๋ฅผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ผ๋‹จ ์ €๋Š” ๊ณต์˜ ํฌ๊ธฐ์™€ ๊ณต์˜ ์ƒ‰๊น”์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ด ๊ฒฝ์šฐ์—๋Š”
๊ฐ™์€ ์ƒ‰๊น”์˜ ๊ณต์˜ ํ•ฉ๊ณผ ๊ฐ™์€ ์‚ฌ์ด์ฆˆ์˜ ๊ณต์˜ ํ•ฉ์„ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


๋Œ€๋ถ€๋ถ„์˜ ์ผ€์ด์Šค๋Š”
(์‚ฌ๋กœ ์žก์„ ์ˆ˜ ์žˆ๋Š” ๊ณต์˜ ํฌ๊ธฐ) = (์ „์ฒด ๊ณต์˜ ํฌ๊ธฐ) - (๊ฐ™์€ ์ปฌ๋Ÿฌ์˜ ํฌ๊ธฐ) - (๊ฐ™์€ ์‚ฌ์ด์ฆˆ์˜ ํฌ๊ธฐ)
๋กœ ์ •๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

๋‹ค๋งŒ, 3๋ฒˆ์˜ ๊ฒฝ์šฐ์—๋Š”
์ด์ „ dp๋ฅผ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ•ด์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.



์ฆ‰,

  1. ๊ณต์˜ ํฌ๊ธฐ, ์ƒ‰๊น”, ์ธ๋ฑ์Šค๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•œ๋‹ค. (info)
  2. ๊ณต์˜ ํฌ๊ธฐ์™€, ์ƒ‰๊น”์— ๋”ฐ๋ผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  3. ์ด์ „ ๊ณต๊ณผ ๋น„๊ตํ•˜์—ฌ ๋™์ผํ•œ ๊ณต์ด๋ฉด, dp๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค.
  4. ๋™์ผํ•œ ๊ณต์ด ์•„๋‹ˆ๋ฉด, (์ „์ฒด ๊ณต์˜ ํฌ๊ธฐ) - (๊ฐ™์€ ์ปฌ๋Ÿฌ์˜ ํฌ๊ธฐ) - (๊ฐ™์€ ์‚ฌ์ด์ฆˆ์˜ ํฌ๊ธฐ)๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ dp์— ์ €์žฅํ•œ๋‹ค.
  5. ๋™์ผํ•œ ๊ณต์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๊ณต์˜ ์ •๋ณด๋ฅผ pre_size, pre_color์— ์ €์žฅํ•œ๋‹ค.
  6. dp๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์ด ํ๋ฆ„๋Œ€๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.



์ถ”๊ฐ€๋กœ!!!
์ถœ๋ ฅ์‹œ์— ์ฃผ์˜ํ•  ์ ์€

๋“ค์–ด์˜จ ๊ณต์˜ ์ธ๋ฑ์Šค์— ๋งž๊ฒŒ, ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ €์˜ ๊ฒฝ์šฐ์—๋Š” sort๋ฅผ ์‚ฌ์šฉํ•˜์˜€๊ธฐ์—, ๊ณต์˜ ๋ฒˆํ˜ธ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ, ๊ธฐ์กด ๊ณต์˜ index๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ์—
์ž…๋ ฅ ์ฒ˜๋ฆฌํ•  ๋•Œ, +[i]๋ฅผ ํ†ตํ•ด์„œ index๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.



์ฐพ์€ ์˜ˆ์™ธ ์ผ€์ด์Šค๋“ค
# 3
# 1 4
# 2 4
# 1 4

# 6  
# 1 5
# 2 6
# 7 6
# 5 6
# 2 6
# 1 6

# 5
# 3 15
# 3 15
# 3 5
# 1 15
# 2 10

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

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.

์™€ ์ด ๋ฌธ์ œ ์–ด๋ ค์› ๋„ค์š”...

์ •๋ ฌํ•ด์„œ ํ‘ธ๋Š”๊ฑฐ๋ž‘ hashMap ์จ์„œ ํ‘ธ๋Š” ๊ฑด ์•Œ์•˜๋Š”๋ฐ,

๊ฐ™์€ ์ƒ‰์ƒ์˜ ๊ฐ™์€ ํฌ๊ธฐ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ์• ๋จน๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ๋ ˆํผ๋Ÿฐ์Šค ๋ณด๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค...

import sys
input = sys.stdin.readline

N = int(input())
arr = []
result = [0] * (N + 1)
color = [0] * (N + 1)
weight = [0] * 2001
for i in range(N):
    e1, e2 = map(int, input().split())
    arr.append((e2,e1,i))
arr.sort()
prefix_sum = [0] * (N + 1)

for i in range(0, N):
    prefix_sum[i] = prefix_sum[i - 1] + arr[i][0]
    
    if i>0 and arr[i][1]==arr[i-1][1] and arr[i][0]==arr[i-1][0]:
        result[arr[i][2]]=result[arr[i-1][2]]
    else:
        result[arr[i][2]]=prefix_sum[i-1]-color[arr[i][1]]-weight[arr[i][0]]
    
    color[arr[i][1]]+=arr[i][0]
    weight[arr[i][0]]+=arr[i][0]

for i in range(0, N):
    print(result[i])

์ƒ‰๊น”๋ณ„๋กœ ํ•ด์‰ฌ๋งต ๋‘๋Š”๊ฑด ํ–ˆ์—ˆ๋Š”๋ฐ..

๋ฌด๊ฒŒ๋ณ„๋กœ ๋˜ ํ•ด์‰ฌ๋งต๋‘ฌ์„œ ๊ฐ™์€ ์ปฌ๋Ÿฌ, ๊ฐ™์€ ๋ฌด๊ฒŒ์ผ ๊ฒฝ์šฐ ๊ฐ’์„ ๋ณต์‚ฌ๋งŒ ํ•œ๋‹ค...

์™€ ๋Œ€๋ฐ•์  ... ๋ฌธ์ œ ์–ด๋ ต๋„ค์š” ...

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.

2 participants