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-alstjr7437 #210

Merged
merged 8 commits into from
Jun 30, 2024
Merged

29-alstjr7437 #210

merged 8 commits into from
Jun 30, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Jun 11, 2024

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

๋‚˜๋ฌด์ž๋ฅด๊ธฐ

๋ฏธ๋ฆฌ๋ณด๊ธฐ ๋ฐฉ์ง€

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

30๋ถ„




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

๋ˆ„๊ฐ€๋ด๋„ (1 โ‰ค N โ‰ค 1,000,000, 1 โ‰ค M โ‰ค 2,000,000,000)
๋ผ์„œ ๊ทธ๋ƒฅ ํ’€๊ฒŒ ๋˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ ๊ฐ™์•˜์ง€๋งŒ
๊ทธ๋ƒฅ๋„ ํ•œ๋ฒˆ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.






๊ทธ๋ƒฅ ์ž๋ฅด๋Š” ๋‚˜๋ฌด ๊ธธ์ด๋ฅผ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ
์ž˜๋ผ์„œ ์–ป์€ ๊ธธ์ด ์ด ํ•ฉ์ด ์›ํ•˜๋Š” ๋‚˜๋ฌด ๊ธธ์ด๋ณด๋‹ค ์ž‘์•„์ง€๋ฉด break
ํ•˜์—ฌ์„œ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ „ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

n, m = map(int, input().split())
tree = list(map(int, input().split()))
result = min(tree)

while True:
    temp = 0
    for i in tree:
        if i > result :
            temp += i - result
    if temp <= m :
        break
    result += 1

print(result)







๋„ค ๋ˆ„๊ฐ€๋ด๋„ ์‹œ๊ฐ„ ์ดˆ๊ณผ์ฃ ???

๋‚˜๋ฌด์˜ ๊ธธ์ด M์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N โ‰ค 1,000,000, 1 โ‰ค M โ‰ค 2,000,000,000)์ธ๋ฐ ์œ„์˜ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด
์˜ˆ๋กœ ๋‚˜๋ฌด ๊ธธ์ด๊ฐ€ 0, 100, 2_000_000_000๊ฐ€ ์žˆ์œผ๋ฉด 0๋ถ€ํ„ฐ 20์–ต๊นŒ์ง€ ๊ณ„์†ํ•ด์„œ ๋Œ์•„์•ผํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.(๊ทธ๋Ÿด์ผ์ด ์—†์ง€๋งŒ)




๊ทธ๋ž˜์„œ ์ƒ๊ฐํ•œ ๋ถ€๋ถ„์ด ๋‚˜๋ฌด์˜ ์ดํ•ฉ ๊ธธ์ด๋Š” ๋˜‘๊ฐ™์ด ๊ตฌํ•˜์ง€๋งŒ

์ž๋ฅด๋Š” ๋‚˜๋ฌด ๊ธธ์ด์˜ ์„ ์ •์„ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ํ•˜๋ฉด ์–ด๋–จ๊นŒ ์˜€์Šต๋‹ˆ๋‹ค!!(์‚ฌ์‹ค ์ฒ˜์Œ์—๋Š” ๋ญ˜ ์ด๋ถ„ํƒ์ƒ‰ํ•ด์•ผํ• ์ง€ ์ž˜ ๊ฐ์ด ์•ˆ์žกํ˜”์Šต๋‹ˆ๋‹ค..)






๊ทธ๋ž˜์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ


start = 1
end = max(tree)
๋กœ ๋‚˜๋ฌด์˜ ์ตœ๋Œ€ ๊ฐ’์„ end๋กœ ์žก๊ณ 
mid๋ฅผ ์ž๋ฅด๋Š” ๋‚˜๋ฌด ๊ฐ’์œผ๋กœ ์žก์•˜์Šต๋‹ˆ๋‹ค.




๊ทธ๋ฆฌ๊ณ  ์œ„ ํ‹€๋ฆฐ ๋ฐฉ์‹๊ณผ ๋˜‘๊ฐ™์ด mid - ๊ฐ ๋‚˜๋ฌด์˜ ๊ฐ’์œผ๋กœ temp์—๋‹ค๊ฐ€ ์ž๋ฅธ ๋‚˜๋ฌด์˜ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ 




์ž๋ฅธ ๋‚˜๋ฌด์˜ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ

  • ์ž๋ฅธ ๋‚˜๋ฌด์˜ ๊ฐ’ >= ์›ํ•˜๋Š” ๋‚˜๋ฌด์˜ ๊ฐ’ : start๋ฅผ mid + 1
  • ์ž๋ฅธ ๋‚˜๋ฌด์˜ ๊ฐ’ < ์›ํ•˜๋Š” ๋‚˜๋ฌด์˜ ๊ฐ’ : end๋ฅผ mid - 1

์œผ๋กœ ์ด๋ถ„ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜์—ฌ์„œ end๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

n, m = map(int, input().split())
tree = list(map(int, input().split()))

start = 1
end = max(tree)

while start <= end:
    mid = (start + end) // 2
    temp = 0
    for i in tree:
        if i > mid:
            temp += i - mid
    
    if temp >= m :
        start = mid + 1
    else :
        end = mid - 1

print(end)

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

์ด๋ถ„ ํƒ์ƒ‰์ธ ๋ถ€๋ถ„์€ ๋ฐ”๋กœ ์•Œ์•˜์ง€๋งŒ ์–ด๋–ป๊ฒŒ ์ด๋ถ„ํƒ์ƒ‰์„ ํ•ด์•ผํ• ์ง€๋„ ์ž˜ ์ƒ๊ฐํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค..
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ž…๊ตญ์‹ฌ์‚ฌ ์ด ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ๋„ ๋˜‘๊ฐ™์€ ๊ณ ๋ฏผ์œผ๋กœ ์ด๋ถ„ํƒ์ƒ‰์˜ mid๋ฅผ ๋ญ˜ ์žก์•„์•ผํ• ์ง€ ๋ชฐ๋ผ์„œ ํ•œ์ฐธ ํ•ด๋งธ๋Š”๋ฐ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด ๋งŽ๋‹ค๊ณ  ์•„์ง ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค.

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.

์ €๋„ ์ด๋ถ„ ํƒ์ƒ‰ left, right ์กฐ๊ฑด์„ ์–ด๋–ป๊ฒŒ ์„ค์ •ํ•ด์•ผ ํ•˜์ง€?

mid ๊ฐ’์€ ์–ด๋–ป๊ฒŒ ์žก์•„์•ผ ํ•˜์ง€? ์— ๋Œ€ํ•ด ๊ณ ๋ฏผ์ด ๋งŽ์•˜์–ด์„œ,

image

์ €๊ธฐ ๋‚˜์™€์žˆ๋Š” ๋งํฌ๋กœ ๊ณต๋ถ€๋ฅผ ํ–ˆ์”๋‹ˆ๋‹ค.

์ด์ œ ๊ฑฐ์˜ ๋ชจ๋“  ์ด๋ถ„ ํƒ์ƒ‰ ๋ฌธ์ œ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜๋‹ˆ ๋‹ค ์ˆ ์ˆ  ํ’€๋ฆฌ๋”๋ผ๊ณ ์š”.

import sys

def input(): return sys.stdin.readline().rsrtip()

N, M = map(int, input().split())
trees = list(map(int,input().split()))

def check(mid, trees, target):
    accumulate = 0
    for tree in trees:
        accumulate += max(0, tree-mid)
    
    if accumulate >= target: return True
    return False

left = 0
right= 1_000_000_001
answer = 0
while left+1 < right:
    mid = (left + right)//2

    if check(mid, trees, M):
        answer = mid
        right = mid

    else: left = mid

print(answer)

๊ทธ ๊ฒฐ๊ณผ ์›ํ๋กœ ํ’€๊ธดํ–ˆ์Šต๋‹ˆ๋‹ค๋งŒ ์ด๊ฑฐ python3์œผ๋กœ๋Š” ์•ˆํ’€๋ฆฌ๋‚˜์š”?

์ข€ ๊นŽ์•„์•ผํ•˜๋‚˜..

image

@9kyo-hwang
Copy link

9kyo-hwang commented Jun 14, 2024

๊ทธ ๊ฒฐ๊ณผ ์›ํ๋กœ ํ’€๊ธดํ–ˆ์Šต๋‹ˆ๋‹ค๋งŒ ์ด๊ฑฐ python3์œผ๋กœ๋Š” ์•ˆํ’€๋ฆฌ๋‚˜์š”?

์ข€ ๊นŽ์•„์•ผํ•˜๋‚˜..

image

python3๋กœ๋„ ํ’€๋ฆฐ๋‹ต๋‹ˆ๋‹น.
image

๊ทผ๋ฐ ์• ์ดˆ์— ๋ฌธ์ œ ํ…Œ์ผ€ ์ž์ฒด๊ฐ€ ๋งŽ์ด ๋นก์‹ ๋“ฏ. C++๋กœ๋„ 156ms๊ฐ€ ๊ฑธ๋ฆฌ๋„ค
image

@alstjr7437
Copy link
Member Author

๊ทธ ๊ฒฐ๊ณผ ์›ํ๋กœ ํ’€๊ธดํ–ˆ์Šต๋‹ˆ๋‹ค๋งŒ ์ด๊ฑฐ python3์œผ๋กœ๋Š” ์•ˆํ’€๋ฆฌ๋‚˜์š”?

์ข€ ๊นŽ์•„์•ผํ•˜๋‚˜..

์ €๋„ python3์œผ๋กœ ๋˜๋”๋ผ๊ตฌ์š” ๋‹ค์‹œ๋ณด๋‹ˆ ์•„์Šฌ์•„์Šฌํ•˜๊ตฐ์š” ใ„ทใ„ท..
image

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

ํŒŒ์ด์ฌ์œผ๋กœ ๊ฐˆ์•„ํƒ€์•ผํ•˜๋‚˜..

์ผ๋‹จ 1๋…„ ๋ฐ˜ ์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ๊ธดํ•œ๋ฐ ๊ธฐ์–ต์€ ์•ˆ๋‚ฌ๊ตฌ์šฉ
์ด๋ถ„ ํƒ์ƒ‰์€ ๋ฐ”๋กœ ์บ์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค๋งŒ...
kotlin Int ๋ฉด ์ˆซ์ž๊ฐ€ ์ปค์„œ ์˜ค๋ฒ„ํ”Œ๋กœ๊ฐ€ ๋‚˜๋”๋ผ๊ตฌ์š”. (์ด๊ฒŒ ์˜ค๋ฒ„ํ”Œ๋กœ๋ฉด ์—๋Ÿฌ๊ฐ€ ์•„๋‹ˆ๋ผ ๊ทธ๋ƒฅ ํ‹€๋ ธ๋‹ค๊ณ  ๋‚˜์˜ค๋Š”๊ฒŒ ๋ฌธ์ œ...)
๊ทธ๋ž˜๋„ ๋ฌธ์ œ์˜ ์ˆซ์ž ๋ฒ”์œ„ ๋ณด๊ณ  ์˜ค๋ฒ„ํ”Œ๋กœ ๋‚˜๊ฒ ๊ตฌ๋‚˜ ์•Œ์•„์ฐจ๋ ค์„œ Long ์œผ๋กœ ๋ณ€๊ฒฝํ•ด ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

์žฌ๊ท€๋กœ ํ’€์—ˆ์–ด์š”.

import java.io.BufferedReader
import java.io.InputStreamReader

private lateinit var br: BufferedReader
private var max: Long = 0

fun main() {
    br = BufferedReader(InputStreamReader(System.`in`))
    val (n, m) = br.readLine().split(" ").map { it.toLong() }

    val trees = br.readLine().split(" ").map { it.toLong() }
    findMax(trees, 0, trees.max(), m)
    println(max)
}

fun findMax(trees: List<Long>, start: Long, end: Long, target: Long) {
    if (start > end) return
    val mid = (start + end) / 2
    val cuts: Long = trees.filter { it > mid }.sumOf { it - mid }
    if (cuts == target) {
        max = mid
        return
    }
    if (cuts > target) {
        max = mid
        return findMax(trees, mid + 1, end, target)
    }
    return findMax(trees, start, mid - 1, target)
}

๊ทธ๋ฆฌ๊ณ  ๋ฏผ์„๋‹˜์ด ํ‘ธ์‹  ๋ฐฉ๋ฒ•์œผ๋กœ ํ•œ ๋ฒˆ ๋” ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

import java.io.BufferedReader
import java.io.InputStreamReader

private lateinit var br: BufferedReader

fun main() {
    br = BufferedReader(InputStreamReader(System.`in`))
    val (n, m) = br.readLine().split(" ").map { it.toLong() }

    val trees = br.readLine().split(" ").map { it.toLong() }
    println(findMax(trees, m).toInt())
}

fun findMax(trees: List<Long>, target: Long): Long {
    var start = 0L
    var end: Long = trees.max()

    while (start <= end) {
        val mid = (start + end) / 2
        val cuts: Long = trees.filter { it > mid }.sumOf { it - mid }
        if (cuts >= target) {
            start = mid + 1
            continue
        }
        end = mid - 1
    }
    return end
}

๊ฒฐ๊ตญ end ๋Š” target ์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒƒ๋“ค ์ค‘์— mid - 1 ๋กœ ์ ์  ์ž‘์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ตœ๋Œ€๊ฐ’์— ๊ฐ€๊นŒ์›Œ์ง„๋‹ค๋Š” ๊ฒƒ์„ ์บ์น˜ํ•œ ํ’€์ด ๋ฐฉ๋ฒ•์ด๊ตฐ์š”! ์žฌ๋ฐŒ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

@tgyuuAn tgyuuAn removed the request for review from H0ngJu June 29, 2024 16:38
@alstjr7437 alstjr7437 merged commit 11e60fc into main Jun 30, 2024
2 checks passed
@alstjr7437 alstjr7437 deleted the 29-alstjr7437 branch June 30, 2024 15:45
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