![](http://codeforces.com/predownloaded/ba/bc/babc0d65dbe3143c2722ff1e61de866d1c7735f7.png)
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.
The first line contains two integers n and k (1 ≤ n ≤ 35000, 1 ≤ 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.
Print the only integer – the maximum total value of all boxes with cakes.
4 1
1 2 2 1
2
7 2
1 3 3 1 4 4 4
5
8 3
7 7 8 7 7 8 1 7
6
In the first example Slastyona has only one box. She has to put all cakes in it, so that there are two types of cakes in the box, so the value is equal to 2.
In the second example it is profitable to put the first two cakes in the first box, and all the rest in the second. There are two distinct types in the first box, and three in the second box then, so the total value is 5.
题意:给你一个长度为n的序列 现在可以分成k部分 求每部分不同数的个数的和的最大值
题解:线段树优化dp
dp[i][j] 表示前j个分成i部分的最大值
1 #pragma comment(linker, "/STACK:102400000,102400000") 2 #include <bits/stdc++.h> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <iostream> 6 #include <cstdlib> 7 #include <cstring> 8 #include <algorithm> 9 #include <cmath> 10 #include <cctype> 11 #include <map> 12 #include <set> 13 #include <queue> 14 #include <bitset> 15 #include <string> 16 #include <complex> 17 #define LL long long 18 #define mod 1000000007 19 using namespace std; 20 int n,k; 21 int a[35003]; 22 int last[35003]; 23 int dp[60][35003]; 24 map<int,int> mp; 25 struct node{ 26 int l,r; 27 int maxn; 28 int add; 29 } tree[500005]; 30 void buildtree(int root ,int left,int right,int p){ 31 tree[root].l=left; 32 tree[root].r=right; 33 tree[root].add=0; 34 tree[root].maxn=0; 35 if(left==right){ 36 tree[root].maxn=dp[p][left-1];//将当前结点的最大值 初始为 前(left-1)个分成p部分的最大值 37 return ; 38 } 39 int mid=(left+right)>>1; 40 buildtree(root<<1,left,mid,p); 41 buildtree(root<<1|1,mid+1,right,p); 42 tree[root].maxn=max(tree[root<<1].maxn,tree[root<<1|1].maxn); 43 } 44 void pushdown(int root) 45 { 46 if(tree[root].add==0) return ; 47 tree[root<<1].add+=tree[root].add; 48 tree[root<<1|1].add+=tree[root].add; 49 tree[root<<1].maxn+=tree[root].add; 50 tree[root<<1|1].maxn+=tree[root].add; 51 tree[root].add=0; 52 } 53 void update(int root,int left,int right,int c) 54 { 55 if(tree[root].l==left&&tree[root].r==right) 56 { 57 tree[root].add+=c; 58 tree[root].maxn+=c; 59 return ; 60 } 61 pushdown(root); 62 int mid=(tree[root].l+tree[root].r)>>1; 63 if(right<=mid) 64 { 65 update(root<<1,left,right,c); 66 } 67 else 68 { 69 if(left>mid) 70 update(root<<1|1,left,right,c); 71 else 72 { 73 update(root<<1,left,mid,c); 74 update(root<<1|1,mid+1,right,c); 75 76 } 77 } 78 tree[root].maxn=max(tree[root<<1].maxn,tree[root<<1|1].maxn); 79 } 80 int query(int root,int left,int right) 81 { 82 if(left>right) 83 return 0; 84 if(tree[root].l==left&&tree[root].r==right) 85 { 86 return tree[root].maxn; 87 } 88 pushdown(root); 89 int mid=(tree[root].l+tree[root].r)>>1; 90 if(right<=mid) 91 return query(root<<1,left,right); 92 else 93 { 94 if(left>mid) 95 return query(root<<1|1,left,right); 96 else 97 return max(query(root<<1,left,mid),query(root<<1|1,mid+1,right)); 98 } 99 } 100 int main() 101 { 102 scanf("%d %d",&n,&k); 103 for(int i=1; i<=n; i++){ 104 scanf("%d",&a[i]); 105 last[i]=mp[a[i]]; 106 mp[a[i]]=i; 107 } 108 for(int i=1;i<=k;i++){ 109 buildtree(1,1,n,i-1);// 110 for(int j=1;j<=n;j++){ 111 update(1,max(last[j]+1,1),j,1); //第j个只能在 (max(last[j]+1,1),j) 做贡献 112 dp[i][j]=query(1,1,j); 113 } 114 } 115 printf("%d ",dp[k][n]); 116 return 0; 117 }