Codeforces 834D - The Bakery(dp+线段树)
思路:dp[i][j]表示到第j个数为止分成i段的最大总和值。
dp[i][j]=max{dp[i-1][x]+c(x+1,j)(i-1≤x≤j-1)},c(x+1,j)表示x+1到j的不同的值。
用线段树维护一下最大值。

上图最后一个点取不到,不解释,不明白请评论。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ls rt<<1,l,m
#define rs rt<<1|1,m+1,r
#define pb push_back
const int INF=0x3f3f3f3f;
const int N=;
int tree[*N],lazy[*N];
int dp[][N];
int now[N],pre[N],a[N];
int i; void push_up(int rt)
{
tree[rt]=max(tree[rt<<],tree[rt<<|]);
} void push_down(int rt)
{
tree[rt<<]+=lazy[rt];
lazy[rt<<]+=lazy[rt];
tree[rt<<|]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
lazy[rt]=;
} void build(int rt,int l,int r)
{
if(l==r)
{
tree[rt]=dp[i-][l];
return ;
}
int m=(l+r)>>;
build(ls);
build(rs);
push_up(rt);
} void update(int p,int delta,int rt,int l,int r)
{
if(l==r)
{
tree[rt]+=delta;
return ;
}
int m=(l+r)>>;
if(p<=m)update(p,delta,ls);
else update(p,delta,rs);
push_up(rt);
} void Update(int L,int R,int delta,int rt,int l,int r)
{
if(L<=l&&r<=R)
{
tree[rt]+=delta;
lazy[rt]+=delta;
return ;
}
if(lazy[rt])push_down(rt);
int m=(l+r)>>;
if(L<=m)Update(L,R,delta,ls);
if(R>m)Update(L,R,delta,rs);
push_up(rt);
} int query(int L,int R,int rt,int l,int r)
{
if(L<=l&&r<=R)return tree[rt];
if(lazy[rt])push_down(rt);
int m=(l+r)>>,ans=;
if(L<=m)ans=max(ans,query(L,R,ls));
if(R>m)ans=max(ans,query(L,R,rs));
return ans;
} int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
memset(now,,sizeof(now));
for(int i=;i<=n;i++)
{
cin>>a[i];
pre[i]=now[a[i]];
now[a[i]]=i;
}
dp[][]=;
for(i=;i<=k;i++)
{
memset(tree,,sizeof(tree));
memset(lazy,,sizeof(lazy));
build(,,n);
for(int j=i;j<=n;j++)
{
Update(pre[j],j-,,,,n);
dp[i][j]=query(i-,j-,,,n);
}
}
cout<<dp[k][n]<<endl;
}
return ;
}
Codeforces 834D - The Bakery(dp+线段树)的更多相关文章
- Codeforces 834D The Bakery 【线段树优化DP】*
Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- Codeforces 833B The Bakery dp线段树
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- ZOJ 3349 Special Subsequence 简单DP + 线段树
同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 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 ...
随机推荐
- JS参差不齐的数组
<html><head> <title>参差不齐的数组</title> <meta charset="utf-8"> & ...
- 搭建Linux-java web运行环境之二:安装mysql
环境 OS:Red Hat Enterprise Linux Server release 7.3 (Maipo) JDK:jdk-7u80-linux-x64.tar.gz Tomcat:apach ...
- java commons.lang3 ArrayUtils使用
java commons.lang3 ArrayUtils使用import org.apache.commons.lang3.ArrayUtils; /** *数组追加数组,不重复 */ public ...
- MySQL Crash Course #16# Chapter 24. Using Cursors + mysql 循环
mysql中游标的使用案例详解(学习笔记)这篇讲得相当直白好懂了. 索引: cursor 基础讲解 mysql 循环 书上的整合代码 cursor 基础讲解 cursor 有点类似于 JDBC 中的 ...
- pyDay7
内容来自廖雪峰的官方网站 1.如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 2.只要是可迭代对象,无论有无下标, ...
- Python 技术点
1.文件操作 1-1 遍历文件夹和文件 import os rootDir = "/path/to/root" for parent, dirnames, filenames in ...
- centos6.7rsync端与window2012服务器实时文件同步
windows文件共享我就不截图了,估计大家都会,我就直接在centos6.7上操作了一.挂载win共享文件夹mount -t cifs -o username=administrator,passw ...
- 01: shell基本使用
目录: 1.1 编写登录欢迎脚本 1.2 重定向与管道操作 1.3 使用shell变量 1.4 特殊的shell变量 1.5 read与echo使用比较 1.1 编写登录欢迎脚本返回顶部 (1)新建脚 ...
- 04: python常用模块
目录: 1.1 时间模块time() 与 datetime() 1.2 random()模块 1.3 os模块 1.4 sys模块 1.5 tarfile用于将文件夹归档成 .tar的文件 1.6 s ...
- vijos 运输计划 - 二分答案 - 差分 - Tarjan
Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球.小 P 掌管一家 ...