D. The Bakery
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.
题目链接:http://codeforces.com/contest/834/problem/D
题意:把n个数分成k段,每段的价值等于这一段内不同数字的个数,求总的最大价值。
可以很快发现这是一个dp,dp[i][j]表示到第i个数字,已经分成了k段的最大价值。
dp[i][j] = max(dp[t][j-1]) (1<= t < i)
可以发现转移不是那么容易,所以我们用到线段树去维护当前位置前面的最大价值。
对于状态i,j,线段树维护的是1~i-1的最大值
对于每一个位置,找到前面最后一个与它数字相同的的位置,把这之间线段树的值都加上1,然后dp[i][j]的值就是j-1到i-1的最大值。
最后答案就是dp[n][k]。
(注意线段树的区间范围是0~n,因为可以直接从0转移过来)
下面给出AC代码:【二维数组改写成一维数组(个人原因,不太喜欢高维度的)】
#include <bits/stdc++.h>
using namespace std;
#define maxn 35010
#define INF 0x3f3f3f3f
int addv[maxn*],Max[maxn*];
int dp[maxn],ql,qr;
int pre[maxn], last[maxn], a[maxn];
void build(int l,int r,int o)
{
addv[o]=;
if(l == r)
{
Max[o]=dp[l];
return;
}
int mid=l+(r-l)/;
build(l,mid,o*);
build(mid+,r,o*+);
Max[o]=max(Max[o*],Max[o*+]);
}
void pushdown(int o)
{
int lc=o*,rc=o*+;
if(addv[o])
{
addv[lc]+=addv[o];
addv[rc]+=addv[o];
Max[lc]+=addv[o];
Max[rc]+=addv[o];
addv[o]=;
}
}
void update(int l,int r,int o)
{
if(ql>qr)
return;
if(ql<=l&&qr>=r)
{
addv[o]++;
Max[o]++;
return;
}
pushdown(o);
int mid=l+(r-l)/;
if(ql<=mid)
update(l,mid,o*);
if(qr>mid)
update(mid+,r,o*+);
Max[o]=max(Max[o*],Max[o*+]);
}
int query(int l,int r,int o)
{
if(ql<=l&&qr>=r)
{
return Max[o];
}
pushdown(o);
int mid=l+(r-l)/;
int best=-INF;
if(ql<=mid)
best=max(best,query(l,mid,o*));
if(qr>mid)
best=max(best,query(mid+,r,o*+));
return best;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
memset(last,-,sizeof(last));
int cnt=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
pre[i]=last[a[i]];
last[a[i]]=i;
if(pre[i]==-)
cnt++;
dp[i]=cnt;
}
for(int kk=;kk<=k;kk++)
{
for(int i=;i<kk-;i++)
dp[i]=-INF;
build(,n,);
for(int i=kk;i<=n;i++)
{
ql=max(,pre[i]),qr=i-;
update(,n,);
ql=,qr=i-;
dp[i]=query(,n,);
}
}
printf("%d\n",dp[n]);
return ;
}
官方题解:
#include <cstdio>
#include <cstring>
#include <map> #define K first
#define V second const int N = ; int last[N], pre[N], dp[N]; int main()
{
int n, m;
while (scanf("%d%d", &n, &m) == ) {
memset(last, , sizeof(last));
for (int i = , a; i <= n; ++ i) {
scanf("%d", &a);
pre[i] = last[a];
last[a] = i;
}
dp[] = ;
for (int i = ; i <= n; ++ i) {
dp[i] = dp[i - ] + !pre[i];
}
for (int k = ; k <= m; ++ k) {
std::map<int, int> c;
c[] = n + ;
int last_dp = dp[k - ];
for (int i = k; i <= n; ++ i) {
int now = ;
while (now + c.rbegin()->V <= last_dp) {
now += c.rbegin()->V;
c.erase(c.rbegin()->K);
}
c.rbegin()->V += now - last_dp;
c[i] = last_dp + ;
auto it = c.upper_bound(pre[i]);
it --;
it->V --;
if (it->V == ) {
c.erase(it->K);
}
last_dp = dp[i];
dp[i] = (n + ) - c.begin()->V;
}
}
printf("%d\n", dp[n]);
}
}