Codeforces Round #426 (Div. 1):B. The Bakery

来源:互联网 发布:淘宝网男手提包 编辑:程序博客网 时间:2024/06/06 02:42

D. The Bakery
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery.

Soon the expenses started to overcome the income, so Slastyona decided to study the sweets market. She learned it's profitable to pack cakes in boxes, and that the more distinct cake types a box contains (let's denote this number as the value of the box), the higher price it has.

She needs to change the production technology! The problem is that the oven chooses the cake types on its own and Slastyona can't affect it. However, she knows the types and order of n cakes the oven is going to bake today. Slastyona has to pack exactly k boxes with cakes today, and she has to put in each box several (at least one) cakes the oven produced one right after another (in other words, she has to put in a box a continuous segment of cakes).

Slastyona wants to maximize the total value of all boxes with cakes. Help her determine this maximum possible total value.

Input

The first line contains two integers n and k (1 ≤ n ≤ 350001 ≤ k ≤ min(n, 50)) – the number of cakes and the number of boxes, respectively.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the types of cakes in the order the oven bakes them.

Output

Print the only integer – the maximum total value of all boxes with cakes.

Examples
input
4 11 2 2 1
output
2
input
7 21 3 3 1 4 4 4
output
5
input
8 37 7 8 7 7 8 1 7
output
6


题意:n个连续的数要分成m段,每段的价值为不同的数字个数,求最大价值和


dp[c][d]表示前c个数分成d段的最大价值,val(a, b)表示区间[a, b]中不同数的个数

转移:dp[c][d] = max(dp[i][d-1]+val(i+1, c)) (1<=i<=c-1)

如果暴力转移的话复杂度是O(kn²)肯定会超时


可以用线段树保存dp[i][d-1]+val(i+1, c)的值,这样每次查询就是log(n)了

保存dp[i][d-1]非常简单,可val(i+1, c)怎么加上去呢?

假设当前遍历完前c个数,那么你只需要知道在这c个数中所有不同的数最后一次出现的位置就好了

对于数字x最后一次出现的位置是y,那么所有的dp[i][d-1] (1<=i<=y-1)都要加上1

因为数字x对所有的val(i, c) (1<=i<=y)都有贡献!(数字x在y往后的位置都没有再出现过)

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[35005], pre[35005], pos[35005], dp[35005][53], tre[138830], temp[138830];void Lazy(int l, int r, int x){int m;m = (l+r)/2;tre[x*2] += temp[x];tre[x*2+1] += temp[x];if(l!=m)temp[x*2] += temp[x];if(r!=m+1)temp[x*2+1] += temp[x];temp[x] = 0;}void Update(int l, int r, int x, int a, int b, int c){int m;if(l>=a && r<=b){tre[x] += c;if(l!=r)temp[x] += c;return;}if(temp[x])Lazy(l, r, x);m = (l+r)/2;if(a<=m)Update(l, m, x*2, a, b, c);if(b>=m+1)Update(m+1, r, x*2+1, a, b, c);tre[x] = max(tre[x*2+1], tre[x*2]);}int Query(int l, int r, int x, int a, int b){int m, now;now = 0;if(l>=a && r<=b)return tre[x];if(temp[x])Lazy(l, r, x);m = (l+r)/2;if(a<=m)now = max(now, Query(l, m, x*2, a, b));if(b>=m+1)now = max(now, Query(m+1, r, x*2+1, a, b));return now;}int main(void){int n, k, i, j;scanf("%d%d", &n, &k);for(i=1;i<=n;i++){scanf("%d", &a[i]);pre[i] = pos[a[i]];pos[a[i]] = i;}for(j=1;j<=k;j++){memset(tre, 0, sizeof(tre));memset(temp, 0, sizeof(temp));for(i=1;i<=n;i++){Update(0, n, 1, i, i, dp[i][j-1]);if(i>=j){Update(0, n, 1, pre[i], i-1, 1);dp[i][j] = Query(0, n, 1, j-1, i-1);}}}printf("%d\n", dp[n][k]);return 0;}






阅读全文
1 0
原创粉丝点击