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. git 常用命令及仓库创建

    一.常用命令 1.添加到本地仓库缓存 git add . 2.查看本地仓库状态 git status 3.提交到本地仓库 git commit -am 'project init' 4.连接线上分支 ...

  2. javascript中string对象方法中的slice、substring、substr的区别联系

    1.slice.substring.snustr均属于String的对象方法,用于截取或提取字符串片段,三者均布破坏原先的字符串,而是以新的字符串返回被提取的部分. <script> va ...

  3. 【JS】window.print打印指定内容

    有时候网页用到打印但是不想打印所有内容,就需要只打印指定内容,下面简单演示下如何打印指定内容 1.在需要打印的指定内容的头部前面加“<!--startprint-->”,在尾部后面加上“& ...

  4. PHP设计者---composer

    Composer 是 PHP5以上 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们.Composer 不是一个包管理器.是的,它涉及 "packages&q ...

  5. 3.4.2 Undefined类型【JavaScript高级程序设计第三版】

    Undefined 类型只有一个值,即特殊的 undefined.在使用 var 声明变量但未对其加以初始化时,这个变量的值就是 undefined,例如: var message; alert(me ...

  6. js三目运算符执行多个条件

    三元运算符的结果语句可以执行多个操作,每个操作用逗号分隔就可以,例子如下: var a=1: a>5?(alert(1),alert(2)):(alert(3),alert(4))

  7. PLC状态机编程-如何在STL中使用状态机

    搞PLC编程多年,一直不知道状态机,学习matlab后,发现状态机编程异常方便,过去很多编程时的疑惑豁然开朗起来.今天跟大家分享一下如何在STL中使用状态机. 下面是用状态机描述的控制任务. 这个状态 ...

  8. tomcat 无论如何都无法创建新的Servers

    1.退出eclipse 2.到[工程目录下 workspace ]/.metadata/.plugins/org.eclipse.core.runtime 3.把org.eclipse.wst.ser ...

  9. 统计输入任意的字符中中英文字母,空格和其他字符的个数 python

    这里用到了三个函数: #判断是否为数字:str.isdigit()#是否为字母:str.isalpha()#是否为空格:str.isspace() def tongji(str): alpha = 0 ...

  10. Leetcode 337. 打家劫舍 III

    题目链接 https://leetcode.com/problems/house-robber-iii/description/ 题目描述 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可 ...