To solve this problem, we need to maximize the total value of exactly k boxes of cakes, where each box contains a contiguous segment of cakes and the value of a box is the number of distinct cake types it contains. The solution involves dynamic programming (DP) combined with a segment tree to efficiently track and update the maximum values as we process each segment.
Approach
- Dynamic Programming (DP) Setup: Use a DP array where
dp[j][i] represents the maximum total value when the first i cakes are divided into j boxes.
- Initialization for One Box: For the first box (j=1), compute the distinct count of cake types from the start up to each position
i.
- Segment Tree for Efficient Updates: For each subsequent box count (from 2 to k), use a segment tree to efficiently manage range updates and maximum queries. This helps track the maximum value of splitting the cakes into the current number of boxes while considering the distinct count of the latest segment.
- Range Updates and Queries: For each position, update the segment tree based on the last occurrence of the current cake type to adjust the distinct count and query the maximum value efficiently.
Solution Code
import sys
from collections import defaultdict
class SegmentTree:
def __init__(self, size):
self.n = size
self.size = 1
while self.size < self.n + 2:
self.size <<= 1
self.tree = [0] * (2 * self.size)
self.lazy = [0] * (2 * self.size)
def push(self, node, l, r):
if self.lazy[node] != 0:
self.tree[node] += self.lazy[node]
if l != r:
self.lazy[2*node] += self.lazy[node]
self.lazy[2*node+1] += self.lazy[node]
self.lazy[node] = 0
def range_add(self, a, b, val, node=1, l=0, r=None):
if r is None:
r = self.size - 1
self.push(node, l, r)
if a > b or l > b or r < a:
return
if a <= l and r <= b:
self.lazy[node] += val
self.push(node, l, r)
return
mid = (l + r) // 2
self.range_add(a, b, val, 2*node, l, mid)
self.range_add(a, b, val, 2*node+1, mid+1, r)
self.tree[node] = max(self.tree[2*node], self.tree[2*node+1])
def point_update(self, pos, val, node=1, l=0, r=None):
if r is None:
r = self.size - 1
self.push(node, l, r)
if l == r:
self.tree[node] = val
return
mid = (l + r) // 2
if pos <= mid:
self.point_update(pos, val, 2*node, l, mid)
else:
self.point_update(pos, val, 2*node+1, mid+1, r)
self.push(2*node, l, mid)
self.push(2*node+1, mid+1, r)
self.tree[node] = max(self.tree[2*node], self.tree[2*node+1])
def range_max(self, a, b, node=1, l=0, r=None):
if r is None:
r = self.size - 1
self.push(node, l, r)
if a > b or l > b or r < a:
return -float('inf')
if a <= l and r <= b:
return self.tree[node]
mid = (l + r) // 2
left = self.range_max(a, b, 2*node, l, mid)
right = self.range_max(a, b, 2*node+1, mid+1, r)
return max(left, right)
def main():
n, k = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
# Initialize dp_prev for j=1
dp_prev = [0] * (n + 1)
freq = defaultdict(int)
distinct = 0
for i in range(1, n+1):
x = a[i-1]
if freq[x] == 0:
distinct += 1
freq[x] += 1
dp_prev[i] = distinct
for j in range(2, k+1):
st = SegmentTree(n + 2)
last_occurrence = dict()
dp_current = [0] * (n + 1)
for i in range(1, n + 1):
x = a[i-1]
prev_last = last_occurrence.get(x, -1)
if prev_last != -1:
st.range_add(prev_last + 1, i-1, 1)
else:
st.range_add(0, i-1, 1)
current_max = st.range_max(0, i-1)
dp_current[i] = current_max if current_max != -float('inf') else 0
st.point_update(i, dp_prev[i] + 1)
last_occurrence[x] = i-1
dp_prev = dp_current
print(dp_prev[n])
if __name__ == "__main__":
main()
Explanation
- Segment Tree: This data structure is used to efficiently manage range updates and maximum queries. It helps track the maximum value of the DP array while considering the distinct count of the current segment.
- Dynamic Programming: The DP array is updated for each possible number of boxes (from 1 to k). For each number of boxes, the segment tree is used to handle the range updates based on the last occurrence of each cake type, ensuring efficient computation of the maximum value for each segment.
- Handling Distinct Counts: The distinct count of each segment is managed by tracking the last occurrence of each cake type and updating the segment tree accordingly to reflect changes in the distinct count when new elements are added.
This approach ensures that the solution efficiently handles the constraints, providing an optimal solution within feasible time limits.