CodeForces 834D The Bakery
题意:将N个数分成K块, 每块的价值为不同数字的个数, 现在求总价值最大。
题解:dp[i][j] 表示 长度为j 且分成 i 块的价值总和。 那么 dp[i][j] = max(dp[i-1][x]+右边的数的贡献) ( 1<=x < j )。 如果每次都从左到右for过去一定会TLE, 所以我们用线段树来优化这个查询的过程, 并且用滚动数组消去第二维空间。
每次新扫到一个数T, 他就会在上一个T的位置+1 --- 现在这个T的位置产生数目加一的贡献。
然后每次扫完一次, 都用DP的值去重新建树。并且将DP的对应位置往右移动一位, 这样下次访问这个位置就是 dp[i-1][x-1] + dp[1][x-j]的价值了。 (j 为现在的位置)。
代码:
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
typedef pair<int,int> pll;
const int N = ;
int dp[N], tree[N<<], lazy[N<<], pos[N], last[N];
void Push_Up(int rt){
tree[rt] = max(tree[rt<<], tree[rt<<|]);
}
void Push_Down(int rt){
if(lazy[rt]){
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
tree[rt<<]+=lazy[rt];
tree[rt<<|]+=lazy[rt];
lazy[rt] = ;
}
}
void Build(int l, int r, int rt){
lazy[rt] = ;
if(l == r){
tree[rt] = dp[l-];
return ;
}
int m = l+r >> ;
Build(lson);
Build(rson);
Push_Up(rt);
}
void Update(int L, int R, int l, int r, int rt){
if(L <= l && r <= R){
lazy[rt]++;
tree[rt]++;
return;
}
int m = l+r >> ;
Push_Down(rt);
if(L <= m) Update(L,R,lson);
if(m < R) Update(L,R,rson);
Push_Up(rt);
}
int Query(int L, int R, int l, int r, int rt){
if(L <= l && r <= R) return tree[rt];
int ans = ;
int m = l+r >> ;
Push_Down(rt);
if(L <= m) ans = max(ans, Query(L,R,lson));
if(m < R) ans = max(ans, Query(L,R,rson));
return ans;
}
int main(){
memset(pos, , sizeof(pos));
memset(last, , sizeof(last));
int n, k, t;
scanf("%d%d",&n,&k);
for(int i = ; i <= n; i++){
scanf("%d",&t);
last[i] = pos[t] + ;
pos[t] = i;
}
for(int i = ; i <= k; i++){
Build(,n,);
for(int j = ; j <= n; j++){
Update(last[j],j,,n,);
dp[j] = Query(,j,,n,);
}
}
printf("%d\n", dp[n]);
return ;
}
CodeForces 834D The Bakery的更多相关文章
- Codeforces 834D The Bakery 【线段树优化DP】*
Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...
- Codeforces 834D - The Bakery(dp+线段树)
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 ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- 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 ...
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- Codeforces 834E The Bakery【枚举+数位dp】
E. Ever-Hungry Krakozyabra time limit per test:1 second memory limit per test:256 megabytes input:st ...
- 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 707B B. Bakery(水题)
题目链接: B. Bakery 题意: 是否存在一条连接特殊和不特殊的边,存在最小值是多少; 思路: 扫一遍所有边: AC代码: #include <iostream> #include ...
随机推荐
- C# 委托(delegate)、泛型委托和Lambda表达式
目录 # 什么是委托 # 委托声明.实例化和调用 1.声明 2.委托的实例化 3.委托实例的调用 4.委托完整的简单示例 #泛型委托 1.Func委托 2.Action委托 3.Predicate委托 ...
- HelloDjango 系列教程:博客从“裸奔”到“有皮肤”
文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在此之前我们已经编写了博客的首页视图,并且配置了 URL 和模板,让 django 能够正确地处理 HTTP 请求并返回合适的 ...
- .net core web api部署到docker
一.创建.net core web api 的Demo 修改部分代码 端口随意指定,ip用星号“*”,方便接下来docker虚拟网络自动分配ip 下一步是Dockerfile文件,如果发现你的项目中没 ...
- Jquery.form异步上传文件常见问题解决
Jquery.form常用方法我就不多说,主要说一下在使用过程中碰到的问题 1.提示 “xxxx” is not define 或者"xxx" is not a function ...
- alluxio源码解析-层次化存储(4)
层次化存储-特性介绍: https://www.alluxio.org/docs/1.6/cn/Tiered-Storage-on-Alluxio.html 引入分层存储后,Alluxio管理的数据块 ...
- Cookie&Session
Cookie&Session 背景:Cookie和Session的原理.作用及如何设置和相关面试. 一.诞生背景 HTTP是无状态的,即服务器无法知道两个请求是否来自同一个浏览器,也就是服务器 ...
- 基于 WPF 模块化架构下的本地化设计实践
背景描述 最近接到一个需求,就是要求我们的 WPF 客户端具备本地化功能,实现中英文多语言界面.刚开始接到这个需求,其实我内心是拒绝的的,但是没办法,需求是永无止境的.所以只能想办法解决这个问题. 首 ...
- asp.net core 从单机到集群
asp.net core 从单机到集群 Intro 这篇文章主要以我的活动室预约的项目作为示例,看一下一个 asp.net core 应用从单机应用到分布式应用需要做什么. 示例项目 活动室预约提供了 ...
- 解决多字段联合逻辑校验问题【享学Spring MVC】
每篇一句 不要像祥林嫂一样,天天抱怨着生活,日日思考着辞职.得罪点说一句:"沦落"到要跟这样的人共事工作,难道自己身上就没有原因? 前言 本以为洋洋洒洒的把Java/Spring数 ...
- springboot入门案例----eclipse编写第一个springboot程序
对于刚入门的springboot的新手来说,学的过程中碰到的一些问题记录下. 首先,配置好Maven环境及本地仓库 之后进入Maven安装目录conf文件夹下的settings.xml配置文件,用No ...