codeforces 834 D. The Bakery(dp + 线段树优化)

题意:

给一个长度为n的序列分成k段,每段的值为这一段不同数字的个数,最大化划分k端的值

$n <= 35000 \(
\)k <= min(n,50)$

思路:

由于k比较小,直接dp就好了

\(dp[i][j]\)选了k段到j的最大值

\(dp[i][j] = max(dp[i-1][k]+diff(k+1,j)) (0 <= k < j)\)

然后用线段树优化一下, 一个数的贡献是上一个相同数字+1的位置到当前位置

#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
#define ls(i) seg[i].lc
#define rs(i) seg[i].rc
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls rt<<1
#define rs (rt<<1|1)
using namespace std;
const int N = 4e4 + 10;
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
return x;
}
int n,k;
int a[N],pre[N],last[N];
int dp[55][N];
int col[N<<2],mx[N<<2];
void pushup(int rt){
mx[rt] = max(mx[ls],mx[rs]);
}
void pushdown(int rt){
if(col[rt]){
col[ls] += col[rt],col[rs] += col[rt];
mx[rs] += col[rt],mx[ls] += col[rt];
col[rt] = 0;
}
}
void update(int L,int R,int v,int l,int r,int rt){
if(L <= l && R >= r){
mx[rt] += v;
col[rt] += v;
return ;
}
pushdown(rt);
int m = l + r>>1;
if(L <= m) update(L,R,v,lson);
if(R > m) update(L,R,v,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L <= l && R >= r) return mx[rt];
pushdown(rt);
int ans = 0;
int m = l +r >>1;
if(L <= m) ans = max(ans,query(L,R,lson));
if(R > m) ans = max(ans,query(L,R,rson));
return ans;
}
int main(){ n = read(),k = read();
for(int i = 2;i <= n + 1;i++){
scanf("%d",a + i);
pre[i] = last[a[i]];
if(pre[i] == 0) pre[i] = 1;
last[a[i]] = i;
}
for(int i = 1;i <= k;i++){
for(int j = 2;j <= n + 1;j++){
update(pre[j],j-1,1,1,n+1,1);
dp[i][j] = query(pre[j],j-1,1,n+1,1);
}
for(int k = 1;k <= ((n+1)<<2);k++) mx[k] = col[k] = 0;
for(int j = 1;j <= n + 1;j++){
update(j,j,dp[i][j],1,n+1,1);
}
}
int ans = 0;
for(int j = 1;j <= n + 1;j++) ans = max(ans,dp[k][j]);
cout<<ans<<endl;
return 0;
}

codeforces 834 D. The Bakery的更多相关文章

  1. Codeforces 833B / B34D The Bakery

    题 OwO http://codeforces.com/contest/833/problem/B 解 首先读入的时候把数据读入到2 ~ n+1的位置(因为线段树处理不到0,所以后移了一格) dp[i ...

  2. Codeforces 834D The Bakery【dp+线段树维护+lazy】

    D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...

  3. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  4. Codeforces 834E The Bakery【枚举+数位dp】

    E. Ever-Hungry Krakozyabra time limit per test:1 second memory limit per test:256 megabytes input:st ...

  5. Codeforces 834D The Bakery - 动态规划 - 线段树

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  6. Codeforces Round #368 (Div. 2) B. Bakery 水题

    B. Bakery 题目连接: http://www.codeforces.com/contest/707/problem/B Description Masha wants to open her ...

  7. Codeforces 834D The Bakery 【线段树优化DP】*

    Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...

  8. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  9. 【最小生成树】Codeforces 707B Bakery

    题目链接: http://codeforces.com/problemset/problem/707/B 题目大意: 给你N个点M条无向边,其中有K个面粉站,现在一个人要在不是面粉站的点上开店,问到面 ...

随机推荐

  1. 触发ionic弹窗区域外的方法

    最近项目需要在页面弹窗的时候需要点击弹窗区域外的地方,其实也就是点击页面HTML就可以关闭弹窗, 首先在controller通过js获取到html的dom节点,然后绑定点击事件,话不多说上代码:   ...

  2. spring-开发需要jar包

    需要的开发包 Spring核心开发包: Commons-logging.jar Spring-beans.jar Spring-context.jar Spring-core.jar Spring-e ...

  3. 基于mybatis设计简单信息管理系统---jsp页面

    1.在设计编辑界面的时候需要有一个下拉的列表页,想要他指定到指定的值: <select id="categoryId" name="categoryId" ...

  4. MongoDB模糊查询

    模糊查询简介MongoDB查询条件可以使用正则表达式,从而实现模糊查询的功能.模糊查询可以使用$regex操作符或直接使用正则表达式对象. MySQL  MongoDB select * from s ...

  5. 即将开始的python之路

    准备开始学py 记录一下 加油

  6. GNU 汇编 协处理器指令

    1. CP15 协处理器 16组寄存器 mcr 写 mrc (rgeister CP15)  读 CP15 到 Register mrc  p15,0,c0,c0,0

  7. 用python写一个类似于linux中的tree

    import os filePath = 'g:/File' j = 0 # 查找的深度计数 def tree(filePath,j): dir_now = os.listdir(filePath) ...

  8. GMT 时间格式转换到 TDateTime (Delphi)

    //GMT 时间格式转换到 TDateTime //忽略时区 function GMT2DateTime(const pSour:PAnsiChar):TDateTime; function GetM ...

  9. zeppelin之连接mysql

    上面的一篇文章,对于zeppelin的使用,只是我们对于数据存储在文件中,每一次对于当我们连接数据库的时候都会有问题,今天刚好 把这个问题解决今天我们刚好来介绍如何使用zeppelin来与数据进行连接 ...

  10. Python装饰器探究——装饰器参数

    Table of Contents 1. 探究装饰器参数 1.1. 编写传参的装饰器 1.2. 理解传参的装饰器 1.3. 传参和不传参的兼容 2. 参考资料 探究装饰器参数 编写传参的装饰器 通常我 ...