-
Notifications
You must be signed in to change notification settings - Fork 0
/
1044.swift
67 lines (57 loc) · 1.43 KB
/
1044.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Foundation
func readList() -> [Int] {
return readLine()!.split(separator: " ").map {Int(String($0))!}
}
var args = readList()
let num = args[0], ex = args[1]
var coins = readList()
struct Info {
var leftSum, rightSum: Int
}
var infos = [Info](repeating: Info(leftSum: 0, rightSum: 0), count: num + 1)
var count = 0, sum = 0
for i in 0..<coins.count {
count += coins[i]
infos[i + 1].leftSum = count
}
sum = count
count = 0
for i in stride(from: infos.count - 2, to: -1, by: -1) {
count += coins[i]
infos[i].rightSum = count
}
// p[0] refers to start, p[1] refers to end
var p = [0, 0]
var min = Int.max, flag = true
if sum >= ex {
while p[1] <= num {
let value = sum - (infos[p[0]].leftSum + infos[p[1]].rightSum)
if value < ex {
p[1] += 1
} else if value > ex {
if value < min {
min = value
}
p[0] += 1
} else {
flag = false
print("\(p[0] + 1)-\(p[1])")
p[0] += 1
}
}
if flag {
p = [0, 0]
while p[1] <= num {
let value = sum - (infos[p[0]].leftSum + infos[p[1]].rightSum)
if value < min {
p[1] += 1
} else if value > min {
p[0] += 1
} else {
flag = false
print("\(p[0] + 1)-\(p[1])")
p[0] += 1
}
}
}
}