-
Notifications
You must be signed in to change notification settings - Fork 31
/
MinPlusConvolution.go
174 lines (158 loc) · 3.33 KB
/
MinPlusConvolution.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
yosupo1()
}
func yosupo1() {
// https://judge.yosupo.jp/problem/min_plus_convolution_convex_convex
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var n, m int
fmt.Fscan(in, &n, &m)
numsA := make([]int, n)
for i := range numsA {
fmt.Fscan(in, &numsA[i])
}
numsB := make([]int, m)
for i := range numsB {
fmt.Fscan(in, &numsB[i])
}
res := MinPlusConvolution(numsA, numsB, true, false)
for _, v := range res {
fmt.Fprint(out, v, " ")
}
}
const INF int = 1e18
// 两个数组`和的卷积`最小值, 即 `C[k] = min{A[i]+B[j]} (i+j==k, 0<=k<=n-1+m-1)`
// 至少一个数组是凸函数.
// 这里凸函数的定义是: f(i+2)+f(i) >= f(i+1)+f(i+1) (0<=i<=n-2)
//
// convexA: numsA是否是凸函数
// convexB: numsB是否是凸函数
func MinPlusConvolution(numsA, numsB []int, convexA, convexB bool) []int {
if !convexA && !convexB {
panic("at least one of A and B must be convex")
}
if convexA && convexB {
return _minPlusConvolutionConvexConvex(numsA, numsB)
}
if convexA && !convexB {
return _minPlusConvolutionArbitraryConvex(numsB, numsA)
}
if convexB && !convexA {
return _minPlusConvolutionArbitraryConvex(numsA, numsB)
}
panic("unreachable")
}
// n,m => 5e5 , 570ms
func _minPlusConvolutionConvexConvex(convexA, convexB []int) []int {
n, m := len(convexA), len(convexB)
if n == 0 && m == 0 {
return nil
}
res := make([]int, n+m-1)
for i := range res {
res[i] = INF
}
for n > 0 && convexA[n-1] == INF {
n--
}
for m > 0 && convexB[m-1] == INF {
m--
}
if n == 0 && m == 0 {
return res
}
a, b := 0, 0
for a < n && convexA[a] == INF {
a++
}
for b < m && convexB[b] == INF {
b++
}
res[a+b] = convexA[a] + convexB[b]
for i := a + b + 1; i < n+m-1; i++ {
if b == m-1 || (a != n-1 && convexA[a+1]+convexB[b] < convexA[a]+convexB[b+1]) {
a++
_chmin(&res[i], convexA[a]+convexB[b])
} else {
b++
_chmin(&res[i], convexA[a]+convexB[b])
}
}
return res
}
// n,m => 5e5 , 630ms
func _minPlusConvolutionArbitraryConvex(arbitrary, convex []int) []int {
n, m := len(arbitrary), len(convex)
if n == 0 && m == 0 {
return nil
}
res := make([]int, n+m-1)
for i := range res {
res[i] = INF
}
for m > 0 && convex[m-1] == INF {
m--
}
if m == 0 {
return res
}
b := 0
for b < m && convex[b] == INF {
b++
}
choose := func(i, j, k int) bool {
if i < k {
return false
}
if i-j >= m-b {
return true
}
return arbitrary[j]+convex[b+i-j] >= arbitrary[k]+convex[b+i-k]
}
minArg := _monotoneMinima(n+m-b-1, n, choose)
for i := 0; i < n+m-b-1; i++ {
x, y := arbitrary[minArg[i]], convex[b+i-minArg[i]]
if x < INF && y < INF {
res[b+i] = x + y
}
}
return res
}
// 寻找二维矩阵中每一行的最小值.
//
// choose(i,j,k) : (i,j) -> (i,k) 是否可以转移极小值.
func _monotoneMinima(H, W int, choose func(i, j, k int) bool) []int {
minCol := make([]int, H)
var dfs func(x1, x2, y1, y2 int)
dfs = func(x1, x2, y1, y2 int) {
if x1 == x2 {
return
}
x := (x1 + x2) >> 1
bestY := y1
for y := y1 + 1; y < y2; y++ {
if choose(x, bestY, y) {
bestY = y
}
}
minCol[x] = bestY
dfs(x1, x, y1, bestY+1)
dfs(x+1, x2, bestY, y2)
}
dfs(0, H, 0, W)
return minCol
}
func _chmin(a *int, b int) bool {
if *a > b {
*a = b
return true
}
return false
}