Codeforces Round #426 (Div. 2)

16 篇文章 0 订阅


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 1
1 2 2 1
output
2
input
7 2
1 3 3 1 4 4 4
output
5
input
8 3
7 7 8 7 7 8 1 7
output
6
Note

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.


题意:给你一个序列,要你将它分成k块,每个块的权值等于里面数字种类,求最大的权值和。

思路:讲真,这题难度有点大,看第一眼很容易想出O(k*n^2)的dp方法。如果想不出来那么你dp可能还没入门。。。dp[i][j]第i个当前末尾为i节点已经分成了j块,那么有动态转移方程:dp[i][j]=max(dp[i][j],dp[x][j-1]+val[x+1][i]),0<x<i,val表示区间权值,那么再进一步想想怎么优化,因为我们要求最大值,所以我们可以考虑用线段树维护最大值,每次扫过一个位置只有一段区间的val会+1,所以每次扫过一个点更新一下线段树即可,因为有k块,所以要建k个线段树,本来我想敲的了,然后我又怕超内存,而且我也懒,又想了一下,因为每次都需要知道当前块数-1的信息,所以先枚举块数后枚举位置来递推,通过滚动每次只需建一个线段树,不过实际总的来说还是建了k棵,不过是用了1棵树的内存,大概就是这样,下面给代码:

#include<bits/stdc++.h> 
using namespace std;
typedef long long LL;
//const LL MOD=1e9+7;
#define inf 0x3f3f3f3f
#define lowerbit(x) x&(-x)
#define maxn 35005
int dp[maxn];
int maxnum[maxn<<2],mark[maxn<<2],Left[maxn],a[maxn],vis[maxn];
void build(int l,int r,int now){
	mark[now]=0;
	if(l==r){
		maxnum[now]=dp[l];
		return;
	}
	int mid=l+r>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	maxnum[now]=max(maxnum[now<<1],maxnum[now<<1|1]);
}
void lazymark(int now){
	maxnum[now << 1] += mark[now];  
    mark[now << 1] += mark[now];  
    maxnum[now << 1 | 1] += mark[now];  
    mark[now << 1 | 1] += mark[now];  
    mark[now] = 0;  
}
void update(int l1, int r1, int l2, int r2, int now){  
    if (l2 <= l1&&r2 >= r1){  
        maxnum[now] += 1;  
        mark[now]++;  
        return;  
    }  
    int mid = l1 + r1 >> 1;  
    if (mark[now])
    	lazymark(now);
    if (l2 <= mid)  
        update(l1, mid, l2, r2, now << 1);  
    if (r2 > mid)  
        update(mid + 1, r1, l2, r2, now << 1 | 1);  
    maxnum[now] = max(maxnum[now << 1], maxnum[now << 1 | 1]);  
}  
int query(int l1,int r1,int l2,int r2,int now){
	if (l2 <= l1&&r2 >= r1)  
        return maxnum[now]; 
	int mid = l1 + r1 >> 1; 
	if (mark[now])
    	lazymark(now);
    int a=0,b=0;
    if(l2<=mid)
    	a=query(l1,mid,l2,r2,now<<1);
    if(r2>mid)
    	b=query(mid+1,r1,l2,r2,now<<1|1);
    return max(a,b);
}
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	memset(vis, 0, sizeof(vis));  
    for (int i = 1; i <= n; i++){  
        scanf("%d", &a[i]);  
        Left[i] = vis[a[i]];  
        vis[a[i]] = i;  
    }  
    for(int i=1;i<=k;i++){
    	build(0,n,1);
		for(int j=i;j<=n;j++){
			update(0,n,Left[j],j-1,1);
			dp[j]=query(0,n,0,j-1,1);
		}
	}
	printf("%d\n",dp[n]);
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值