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.ios
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.c++
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.ide
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.ui
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the types of cakes in the order the oven bakes them.this
Print the only integer – the maximum total value of all boxes with cakes.spa
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.rest
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.code
題目大意:把長度爲n的數組分紅k個連續的部分,求每部分不一樣數字的個數相加的最大和是多少?blog
解題思路:這是一個求顏色的貢獻的問題,可以知道dp[i][j=max{dp[i-1][x]+剩下數字的貢獻|1<=x<j},其中剩下數字的貢獻長度就是 p+1~x,p知足a[p]==a[x],用線段樹剩下數字的貢獻距離進行維護,不斷的更新最大值便可。思路來自其餘博客,詳情見代碼。
AC代碼:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <bits/stdc++.h> 6 using namespace std; 7 const int maxn=350005; 8 int dp[55][maxn]; 9 int tree[maxn<<2],lazy[maxn]; 10 int pos[maxn],pre[maxn]; 11 int n,k; 12 void push_down(int t) 13 { 14 if(tree[t]) 15 { 16 tree[t<<1|1]+=lazy[t]; 17 tree[t<<1]+=lazy[t]; 18 lazy[t<<1|1]+=lazy[t]; 19 lazy[t<<1]+=lazy[t]; 20 lazy[t]=0; 21 } 22 } 23 void push_up(int t) 24 { 25 tree[t]=max(tree[t<<1],tree[t<<1|1]); 26 } 27 void build(int k,int l,int r,int t) 28 { 29 lazy[t]=0; 30 if(l==r) 31 { 32 tree[t]=dp[k][l-1]; 33 34 return ; 35 } 36 int mid=(l+r)/2; 37 build(k,l,mid,t<<1); 38 build(k,mid+1,r,t<<1|1); 39 push_up(t); 40 } 41 42 void update(int L,int R,int l,int r,int t) 43 { 44 if(L<=l&&r<=R) 45 { 46 tree[t]++; 47 lazy[t]++; 48 return ; 49 } 50 int mid=(l+r)/2; 51 push_down(t); 52 if(L>=mid) update(L,R,l,mid,t<<1); 53 if(R<mid) update(L,R,mid+1,r,t<<1|1); 54 push_up(t); 55 } 56 int query(int L,int R,int l,int r,int t) 57 { 58 if(L<=l&&r<=R) 59 { 60 return tree[t]; 61 } 62 int mid=(l+r)/2; 63 push_down(t); 64 int ans=0; 65 if(L<=mid) ans=max(ans,query(L,R,l,mid,t<<1)); 66 if(R>mid) ans=max(ans,query(L,R,mid+1,r,t<<1|1)); 67 push_up(t); 68 return ans; 69 } 70 int main() 71 { 72 while(~scanf("%d%d",&n,&k)) 73 { 74 memset(pre,0,sizeof(pre)); 75 memset(pos,0,sizeof(pos)); 76 memset(tree,0,sizeof(tree)); 77 memset(lazy,0,sizeof(lazy)); 78 int x; 79 for(int i=1;i<=n;i++) 80 { 81 scanf("%d",&x); 82 pre[i]=pos[x]+1; 83 pos[x]=i; 84 } 85 for(int i=1;i<=k;i++) 86 { 87 build(i-1,1,n,1); 88 for(int j=1;j<=n;j++) 89 { 90 update(pre[j],j,1,n,1); 91 dp[i][j]=query(1,j,1,n,1); 92 } 93 } 94 printf("%d\n",dp[k][n]); 95 } 96 return 0; 97 }