834D - The Bakery

思路: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+线段树)的更多相关文章

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

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

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

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

  3. Codeforces 833B The Bakery dp线段树

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

  4. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  5. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  6. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  7. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  8. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. 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 ...

随机推荐

  1. Learning to Rank算法介绍:RankNet,LambdaRank,LambdaMart

    之前的博客:http://www.cnblogs.com/bentuwuying/p/6681943.html中简单介绍了Learning to Rank的基本原理,也讲到了Learning to R ...

  2. 错误源:.net SqlClient data provider

    下午在做毕业设计的时候,想删除数据库的一条数据,结果发现删除的时候老是出现 ======错误源:.net SqlClient data provider==== 这样的错误:本来以为是我还在运行着项目 ...

  3. 【转】Java学习:Java中的线程之线程间的通信

    hello各位小伙伴 今天我们来搞一下 线程之间的通信 ( • ̀ω•́ )✧ 让线程按照我们的想法来执行 两个线程间的通信 这是我们之前的线程. 执行效果:谁抢到资源,谁运行~ 实现线程交替执行: ...

  4. python json-json.loads()函数中的字符串需要是严格的json串格式,不能包含单引号

    先看下json的dumps()和loads()函数的定义 json.dumps():将一个Python对象编码成JSON字符串.把字典对象转换成json串 json.loads():将JSON格式字符 ...

  5. 2018-2019-2 20165209 《网络对抗技术》Exp4:恶意代码分析

    2018-2019-2 20165209 <网络对抗技术>Exp4:恶意代码分析 1 基础问题回答和实验内容 1.1基础问题回答 如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监 ...

  6. centos7源码编译安装Subversion 1.9.5

    svn是Subversion的简称,是一个开放源代码的版本控制系统.svn有两种运行方式:1.独立服务器(svn://xxx.xxx/xxx) 2.借助apache(http://svn.xxx.xx ...

  7. python3.4学习笔记(二十一) python实现指定字符串补全空格、前面填充0的方法

    python3.4学习笔记(二十一) python实现指定字符串补全空格.前面填充0的方法 Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0.zfill()方法语法:s ...

  8. zookeeper与卡夫卡集群搭建

    首先这片博客没有任何理论性的东西,只是详细说明kafka与zookeeper集群的搭建过程,需要三台linux服务器. java环境变量设置 zookeeper集群搭建 kafka集群搭建 java环 ...

  9. Git 的安装步骤

    Git 的安装步骤 一.下载Git Git 的官网:https://git-scm.com/ 在 Git 的官网中点击Downloads,进入如下页面: 选择对应的操作系统,以博主为例,点击Windo ...

  10. 更改 Centos 6 的 yum 源

    1.查看当前使用的源: yum repolist all 阿里源网址,使用方法点右边的帮助可以看到:https://opsx.alibaba.com/mirror 2.更改源: 第一步:备份你的原镜像 ...