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 ...
随机推荐
- 【iOS】receiver type *** for instance message is a forward declaration
错误原因:没有引入相关的头文件 http://stackoverflow.com/questions/8815200/receiver-type-for-instance-message-is-a-f ...
- 反应式微服务框架Flower
Flower是一个构建在Akka上的反应式微服务框架,开发者只需要针对每一个细粒度的业务功能开发一个Service服务,并将这些Service按照业务流程进行可视化编排,即可得到一个反应式系统. 即时 ...
- gRPC的简单使用
目录 前言 gRPC的简单介绍 基本用法 服务的定义 服务端代码编写 客户端代码编写 运行效果 服务治理(注册与发现) .NET Core 2.x 和 .NET Core 3.0的细微区别 扩展阅读 ...
- 【0728 | 预习】第三篇 Python基础
第三篇 Python基础预习 Part 1 变量 一.什么是变量? 二.为什么要有变量? 三.定义变量 四.变量的组成 五.变量名的命名规范 六.变量名的两种风格 Part 2 常量 Part 3 P ...
- 在Vue 中使用Typescript
Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...
- Docker 前沿概述
目录 Docker 前沿概述 什么是Docker? Docker的基本概念 容器(Container) -- 镜像运行时的实体 镜像(Image) -- 一个特殊的文件系统 仓库(Repository ...
- 我的C语言学习1
学习是快乐的,尤其是从之前看到一个程序的一头雾水到大致懂了是怎么回事,这个过程是兴奋开心的,让我不断的前进,不能自拔,今天就要结束,总结一下. 1.1-第一个C语言 #include<stdio ...
- CodeForces 29D Ant on the Tree
洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译. 这题有\(\bm3\)种解法,但只有一种是正解(这不是废话嘛). 方法\(\bm1\):最近公共祖先LCA(正 ...
- springboot集成redis实现消息发布订阅模式-双通道(跨多服务器)
基础配置参考https://blog.csdn.net/llll234/article/details/80966952 查看了基础配置那么会遇到一下几个问题: 1.实际应用中可能会订阅多个通道,而一 ...
- npm执行命令行报错
今天在学习react-router时候使用命令npm start 报了一个错误 npm ERR! missing script: start npm ERR! A complete log of th ...