Codeforces Round #426 (Div. 1):B. 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


题意: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、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值