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. 深入理解java虚拟机学习笔记(二)垃圾回收策略

    上篇文章介绍了JVM内存模型的相关知识,其实还有些内容可以更深入的介绍下,比如运行时常量池的动态插入,直接内存等,后期抽空再完善下上篇博客,今天来介绍下JVM中的一些垃圾回收策略.        一. ...

  2. Linux文件服务器实战(系统用户)

    ftp匿名用户设置完成之后任何人都可以访问服务器端文件,目录,甚至可以修改删除文件和目录,,那如何存放私密文件并保证文件或者目录专属于拥有者呢,就需要使用vsftp系统用户来实现了. 1.在linux ...

  3. python 获取项目的根路径

    root_path = os.path.abspath(os.path.dirname(__file__)).split('shippingSchedule')[0] shippingSchedule ...

  4. 【转载】C++中的static关键字的总结

    本文前半部分转自:博主chao_yu 本文后半部分转自:博主VincentCZW 静态变量作用范围在一个文件内,程序开始时分配空间,结束时释放空间,默认初始化为0,使用时可以改变其值. 静态变量或静态 ...

  5. LeetCode summary

    https://www.programcreek.com/2013/08/leetcode-problem-classification/ https://medium.com/algorithms- ...

  6. Choosing Capital for Treeland CodeForces - 219D (树形DP)

    传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional  ...

  7. HDU 6274 二分+预处理(CCPC K题

    #include"bits/stdc++.h" #define db double #define ll long long #define vec vector<ll> ...

  8. 使用python制作神经网络——搭建框架

    一.神经网络的大体结构可分为三个函数,分别如下: 1.初始化函数 设定输入层节点,隐藏层节点和输出层节点的数量. 2.训练 学习给定训练集样本后,优化权重. 3.查询 给定输入,从输出节点给出答案 所 ...

  9. SVN 的基本用法

    克隆远程库 # svn checkout $URL --username=$userName 显示库信息 # svn info 显示库状态 # svn status 将文件纳入版本管理 # svn a ...

  10. 安装python 第三方库遇到的安装问题 microsoft visual studio c++ 10.0 is required,Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?

    问题一: microsoft visual studio c++ 10.0 is required 安装scrapy时候出现需要vc c++ 10,有时安装其他也会有. 解决方法:安装vc 2010, ...