20251121 baekjoon

This commit is contained in:
songyc macbook 2025-11-21 21:41:44 +09:00
parent 97f863619f
commit 537c5683f4
2 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,33 @@
if let T = Int(readLine() ?? ""),
let n = Int(readLine() ?? ""),
let inputA = readLine(),
let A = inputA.split(separator: " ").compactMap({Int($0)}) as? [Int],
let m = Int(readLine() ?? ""),
let inputB = readLine(),
let B = inputB.split(separator: " ").compactMap({Int($0)}) as? [Int],
A.count == n , B.count == m
{
var countSumB = [Int: Int]()
for i in 0..<m {
var currentSumB = 0
for j in i..<m {
currentSumB += B[j]
countSumB[currentSumB, default: 0] += 1
}
}
var ans = 0
for i in 0..<n {
var currentSumA = 0
for j in i..<n {
currentSumA += A[j]
let target = T - currentSumA
if let cnt = countSumB[target] {
ans += cnt
}
}
}
print(ans)
}

View File

@ -0,0 +1,65 @@
if let T = Int(readLine() ?? ""),
let n = Int(readLine() ?? ""),
let inputA = readLine(),
let A = inputA.split(separator: " ").compactMap({Int($0)}) as? [Int],
let m = Int(readLine() ?? ""),
let inputB = readLine(),
let B = inputB.split(separator: " ").compactMap({Int($0)}) as? [Int],
A.count == n , B.count == m
{
var sumA: [Int] = []
for i in 0..<n {
var currentSumA = 0
for j in i..<n {
currentSumA += A[j]
sumA.append(currentSumA)
}
}
sumA.sort()
var sumB: [Int] = []
for i in 0..<m {
var currentSumB = 0
for j in i..<m {
currentSumB += B[j]
sumB.append(currentSumB)
}
}
sumB.sort()
var left = 0
var right = sumB.count-1
var ans = 0
while left < sumA.count && right >= 0 {
let temp = sumA[left] + sumB[right]
if T > temp {
left += 1
}
else if T < temp {
right -= 1
}
else {
var sameCountA = 0
let currentSumA = sumA[left]
while left < sumA.count && sumA[left] == currentSumA {
sameCountA += 1
left += 1
}
var sameCountB = 0
let currentSumB = sumB[right]
while right >= 0 && sumB[right] == currentSumB {
sameCountB += 1
right -= 1
}
ans += sameCountA * sameCountB
}
}
print(ans)
}