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 ...
随机推荐
- photoshop打造超酷炫火焰人像效果
效果图看上去非常的酷.制作方法跟火焰字过程差不多.唯一不同的是前期的处理,需要用滤镜把人物轮廓路径找出来,去色后再用制作火焰的过程制作.最后把最好的火焰叠加到人物上面,适当用蒙版控制区域即可.原图 最 ...
- #C++初学记录(算法2)
A - Game 23 Polycarp plays "Game 23". Initially he has a number n and his goal is to trans ...
- OAuth 白话简明教程 2.授权码模式(Authorization Code)
转自:http://www.cftea.com/c/2016/11/6703.asp OAuth 白话简明教程 1.简述 OAuth 白话简明教程 2.授权码模式(Authorization Code ...
- SQL: 左连接,右连接,内连接,左外连接,右外连接,完全连接
例子: ---------------------- --------------------------- a表 id name b表 id job parent_id 1 张三 ...
- linux常用命令:Linux 文件属性详解
Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loc ...
- 有趣的js匿名函数写法(function嵌套)
例子没有什么实际意义,只能做为思路参考 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...
- 2016NOI冬令营day4
上午:随机算法/近似算法与随机算法的分析方法与应用实例 不懂,完全滑水QAQ :( 下午:计算理论与NP问题 只有讲2-sat和3-sat的时候能听懂,其他的基本都在滑水:( 晚上说是什么中学生学术训 ...
- Python3 爬取微信好友基本信息,并进行数据清洗
Python3 爬取微信好友基本信息,并进行数据清洗 1,登录获取好友基础信息: 好友的获取方法为get_friends,将会返回完整的好友列表. 其中每个好友为一个字典 列表的第一项为本人的账号信息 ...
- C++ 文件大小格式化
#include <iostream> #include <windows.h> using namespace std; /*文件大小格式化 *param [in] dwSi ...
- C++ 一串数字三位一节,用逗号隔开表示
#include <iostream> #include <string> #include <sstream> using namespace std; stri ...