baekjoon 20260408
This commit is contained in:
parent
11b4b82914
commit
f530361b18
5
code_study/Baekjoon/python/4504.py
Normal file
5
code_study/Baekjoon/python/4504.py
Normal file
@ -0,0 +1,5 @@
|
||||
res = []
|
||||
n = int(input())
|
||||
while (m := int(input())) != 0 :
|
||||
res.append(f"{m} is NOT a multiple of {n}." if m%n else f"{m} is a multiple of {n}.")
|
||||
print("\n".join(res))
|
||||
91
code_study/Baekjoon/swift/20303.swift
Normal file
91
code_study/Baekjoon/swift/20303.swift
Normal file
@ -0,0 +1,91 @@
|
||||
import Foundation
|
||||
|
||||
final class FileIO {
|
||||
private let buffer:[UInt8]
|
||||
private var index: Int = 0
|
||||
|
||||
init(fileHandle: FileHandle = FileHandle.standardInput) {
|
||||
|
||||
buffer = Array(try! fileHandle.readToEnd()!) + [UInt8(0)] // 인덱스 범위 초과 방지
|
||||
}
|
||||
|
||||
@inline(__always) private func read() -> UInt8 {
|
||||
defer { index += 1 }
|
||||
return buffer[index]
|
||||
}
|
||||
|
||||
@inline(__always) func readInt() -> Int {
|
||||
var sum = 0
|
||||
var now = read()
|
||||
var isPositive = true
|
||||
|
||||
while now == 10 || now == 32 { now = read() } // 공백과 줄바꿈 무시
|
||||
if now == 45 { isPositive = false; now = read() } // 음수 처리
|
||||
|
||||
while now >= 48, now <= 57 {
|
||||
sum = sum * 10 + Int(now-48)
|
||||
now = read()
|
||||
}
|
||||
|
||||
return sum * (isPositive ? 1 : -1)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
let fp = FileIO()
|
||||
|
||||
let N = fp.readInt(), M = fp.readInt(), K = fp.readInt()
|
||||
var candy: [Int] = [0]
|
||||
for _ in 1...N {
|
||||
candy.append(fp.readInt())
|
||||
}
|
||||
|
||||
var roots: [Int] = (0...N).map{Int($0)}
|
||||
var rank: [Int] = Array(repeating: 0, count: N+1)
|
||||
|
||||
func find(_ x: Int) -> Int {
|
||||
if x != roots[x] { roots[x] = find(roots[x]) }
|
||||
return roots[x]
|
||||
}
|
||||
|
||||
func union(_ x: Int, _ y: Int) {
|
||||
var a = find(x), b = find(y)
|
||||
|
||||
if a == b { return }
|
||||
|
||||
if rank[a] < rank[b] { swap(&a, &b) }
|
||||
|
||||
roots[b] = a
|
||||
|
||||
if rank[a] == rank[b] { rank[a] += 1 }
|
||||
}
|
||||
|
||||
for _ in 0..<M { union(fp.readInt(), fp.readInt()) }
|
||||
|
||||
var counter: [Int: Int] = [:]
|
||||
for n in 1...N {
|
||||
let r = find(n)
|
||||
counter[r] = (counter[r] ?? 0) + 1
|
||||
if n == r { continue }
|
||||
candy[r] += candy[n]
|
||||
}
|
||||
|
||||
var pair: [(nums: Int, total_candy: Int)] = []
|
||||
|
||||
for (group, nums) in counter {
|
||||
pair.append((nums: nums, total_candy: candy[group]))
|
||||
}
|
||||
|
||||
var dp: [Int] = Array(repeating: 0, count: K)
|
||||
for i in 0..<pair.count {
|
||||
let k = pair[i].nums, c = pair[i].total_candy
|
||||
|
||||
for j in stride(from: K-1, through: k, by: -1) {
|
||||
dp[j] = max(dp[j], dp[j-k] + c)
|
||||
}
|
||||
}
|
||||
|
||||
print(dp[K-1])
|
||||
}
|
||||
|
||||
main()
|
||||
Loading…
x
Reference in New Issue
Block a user