codeforces The Bakery(n个数划分k区间,权值为区间不同数,问总权值最大)
来源:互联网 发布:java truststore 编辑:程序博客网 时间:2024/05/22 14:38
B. The Bakery
time limit per test2.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ≤ 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.
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段,每段的价值是其内部权值的种类数,让你最大化所有段的价值之和
很容易想到dp,那么 dp[i][j]=max(dp[i-1][k]+w[k~j]的权值)
i为划分组数 j为位置
首先枚举 划分的 组数i,求得 dp前面所有阶段的 dp[i-1][j] ,那么当前阶段的转移就是建造一棵线段树,上面是每个位置 划分i-1段的最大权值。
然后更新当前值给前面所有值带来的影响,其实影响就是 如果给所有分组能涉及到上一个j这个位置的数字的上一个位置的权值和+1,否则的话当前的值会和这个值上一个的位置分到一组,有重复,权值不加,然后再查询一下 以前的所有区间里面哪个最大,其实就是很常规的dp思想, 先分 j-1 组 再看加上当前权值的影响后,哪个权值大。
#include <bits/stdc++.h>using namespace std;const int maxn=35500;int dp[55][maxn];int pre[4*maxn],now[4*maxn];int sum[4*maxn];int add[4*maxn];void push_up(int x){ sum[x]=max(sum[x<<1],sum[x<<1|1]);}void push_down(int x){ if(add[x]) { add[x<<1]+=add[x],add[x<<1|1]+=add[x]; sum[x<<1]+=add[x];sum[x<<1|1]+=add[x]; add[x]=0; }}void build(int id,int l,int r,int rt){ sum[rt]=0; add[rt]=0; if(l==r) { sum[rt]=dp[id][l-1]; return ; } int mid=(l+r)>>1; build(id,l,mid,rt<<1); build(id,mid+1,r,rt<<1|1); push_up(rt);}void update(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r) { sum[rt]++; add[rt]++; return ; } push_down(rt); int mid=(l+r)>>1; if(L<=mid) update(L,R,l,mid,rt<<1); if(R>mid) update(L,R,mid+1,r,rt<<1|1); push_up(rt);}int query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) { return sum[rt]; } push_down(rt); int mid=(l+r)>>1; int res=0,res1=0; if(L<=mid) res=query(L,R,l,mid,rt<<1); if(R>mid) res1=query(L,R,mid+1,r,rt<<1|1); push_up(rt); return max(res,res1);}int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { int d; scanf("%d",&d); pre[i]=now[d]+1; now[d]=i; } for(int i=1;i<=m;i++) { build(i-1,1,n,1); for(int j=1;j<=n;j++) { update(pre[j],j,1,n,1); dp[i][j]=query(1,j,1,n,1); } } printf("%d\n",dp[m][n] );}
- codeforces The Bakery(n个数划分k区间,权值为区间不同数,问总权值最大)
- hdu 5919 主席树(区间不同数个数+区间第k个数)
- POJ 2761-Feed the dogs(划分树)求区间内第k小的数
- 【HDU5751 BestCoder Round 84E】【FFT + 线段树求最值】Eades 最大数出现次数为[1~n]的区间个数
- 对n个数字进行排序,其中两两不同的数字的个数为k,n远远大于k,而n的取值区间长度超过了内存的大小,时间复杂度最小
- NYOJ 746 - 正整数n划分为m段,求m段的最大乘积 【区间DP】
- 区间-数的划分
- 划分树(求区间第k小值)
- SOJ-3010(划分树求区间内的第k个数)
- HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)
- 区间第k大的数 划分树模板
- 找出N个数中最大的K个不同的浮点数
- SPOJ DQUERY 区间内不同数的个数 主席树
- SPOJ-DQUERY-主席树求区间不同数个数模板
- SPOJ DQUERY 区间内不同数的个数
- SPOJ D-query(区间不同的数的个数)
- k倍区间 O(N)
- 【Codeforces Testing Round 12A】【讨论 边界元素映射】Divisibility 区间范围内k倍数的数的个数
- 新的起点,新的开始!
- 【Linux学习笔记】Chapter 7 Linux文件与目录管理_笔记
- Internet Download Manager IDM 如何抓取网页 整个网页内容
- Python:lambda、map、filter、reduce
- AI和00后
- codeforces The Bakery(n个数划分k区间,权值为区间不同数,问总权值最大)
- react native中的属性、状态、样式的理解
- 商家入驻小程序接系统开发
- 51nod 1116 K进制下的大数
- 快速幂+快速矩阵幂学习
- Unity3D中的SendMessage使用(消息推送)
- java.util.ConcurrentModificationException:并发修改异常!
- 系统调用和进程切换时的寄存器信息保存在哪里?
- 递归、迭代、到动态规划