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. Mysql5.7创建存储过程中调用自定义函数报错Not allowed to return a result set from a function

    因为很多存储过程都会共用一段sql语句,所以我把共用的sql封装成一个自定义函数 AddCapital(); 然后通过存储过程调用,创建存储过程会报错1415,Not allowed to retur ...

  2. 浅谈MySQL字符集

      Preface       MySQL use character set & collation to organize the different charater.It provid ...

  3. django+xadmin在线教育平台(八)

    4-5 user modesl.py设计 循环引用: 设计app时每个app都有model   mark 如图:我们在user中定义usercourse记录用户学习的课程.会有两个外键:user和co ...

  4. JQuery制作网页—— 第一章 JavaScript基础

    1. JavaScript(弱类型语言):是一种描述性语言,也是一种基于对象(Object)和事件驱动(Event Driven)的,并具有安全性能的脚本语言. 特点:1.主要用来在HTML页面中添加 ...

  5. html基础之遗忘篇

    a链接: ①a的href指向压缩文件可以下载压缩文件. ②a链接的打开方式可以在head内使用<base target="_blank">来整体控制打开方式. 字符实体 ...

  6. 各种Nand的总结

    1. 微观 NAND闪存NAND是非易失性存储技术,NAND闪存由多个存放以位(bit)为单位的单元构成,这些位通过电荷被打开或关闭,如何组织这些开关单元来储存在SSD上的数据,也决定了NAND闪存的 ...

  7. Windows GitLab使用全过程

    1.首先安装Git 1.1.下载网站: https://git-for-windows.github.io/ 1.2.安装Git参考网站 http://blog.csdn.net/u012614287 ...

  8. Android 布局错乱 Android花屏

    最近做项目,妈的,有个一个很难受的bug. 这个bug ,自己这里没有手机,没有办法复现,找到了手机之后.解决了. 我先给大家看下什么叫布局错乱,花屏: 来张正常的图片: 正常情况下是这样的.然后, ...

  9. 剑指Offer - 九度1506 - 求1+2+3+...+n

    剑指Offer - 九度1506 - 求1+2+3+...+n2013-11-29 19:22 题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switc ...

  10. CodeIgniter学习笔记三:扩展CI的控制器、模型

    一.扩展CI中的控制器 有时需要对CI中的控制器作统一操作,如进行登录和权限验证,这时就可以通过扩展CI控制器来实现. 扩展CI控制器只需要在application/core文件夹中建一个继承自CI_ ...