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的更多相关文章

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

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

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

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

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

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

  5. CodeForces 834D The Bakery(线段树优化DP)

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

  6. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  7. Codeforces 834E The Bakery【枚举+数位dp】

    E. Ever-Hungry Krakozyabra time limit per test:1 second memory limit per test:256 megabytes input:st ...

  8. Codeforces 833B The Bakery dp线段树

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

  9. codeforces 707B B. Bakery(水题)

    题目链接: B. Bakery 题意: 是否存在一条连接特殊和不特殊的边,存在最小值是多少; 思路: 扫一遍所有边: AC代码: #include <iostream> #include ...

随机推荐

  1. Eclipse Other Projects小问题

    Eclipse 不知什么时候多了个 "Other Projects" 文件夹,所有的项目又多了一层目录,如图所示: 虽然对功能没任何影响,但每次打开有些麻烦,多少感觉有些不爽…… ...

  2. [Hei.Captcha] Asp.Net Core 跨平台验证码实现

    写在前面 说起来比较丢脸.我们有个手机的验证码发送逻辑需要使用验证码,这块本来项目里面就有验证码绘制逻辑,.Net Framework的,使用的包是System.Drawing,我把这验证码绘制逻辑复 ...

  3. oracle的开窗函数

    原创 select * from (select province, commodity, sum(price), ROW_NUMBER() OVER(PARTITION BY province  o ...

  4. Ubuntu 执行chmod -R 777 / 挽救方法

    mgj怎么会有堪比rm -rf /*这样神奇的命令,本想着把当前目录下的权限改为777,没想到把整个/目录下全设成777了,直觉告诉我好像哪里有些不对劲,好在一顿xjb折腾最终弄好了,应该没啥大问题, ...

  5. 大型系列课程之-七夕告白之旅vbs篇

    也许,世间所有的美好的东西,都是需要仪式感的,遇到了一年一度的七夕节,怎么过这个节日,成了很多心中有爱的人关注的事情,七夕不浪漫,人间不值得,七夕,发源于中国,这个美好的节日,来自动人的神话故事传说牛 ...

  6. MobaXterm:远程终端登录软件封神选手

    提到SSH.Telnet等远程终端登录,我相信很多人想到的都是PuTTY PuTTY通常用于Windows,但实际上可以多平台运行,因此不表达为"Windows下的远程终端登录" ...

  7. 记一次IDEA 打包环境JDK版本和生产环境JDK版本不一致引发的血案

    问题描述: 本地开发环境idea中能正常运行项目,而idea打war包到Linux服务器的Tomcat下却不能正常运行,报如下错误: 09-Aug-2019 08:56:06.878 SEVERE [ ...

  8. alluxio源码解析-层次化存储(4)

    层次化存储-特性介绍: https://www.alluxio.org/docs/1.6/cn/Tiered-Storage-on-Alluxio.html 引入分层存储后,Alluxio管理的数据块 ...

  9. Python 学习笔记(6)— 字符串格式化

    字符串格式化处理 远古写法 以前通常使用运算符号 % ,%s 插入的值 String 类型,%.3f 指插入的值为包含 3 位小数的浮点数: format1 = "%s, %s!" ...

  10. JAVA-SpringMVC 概述及组件介绍

    一.SpringMVC概述 SpringMVC是一个WEB层.控制层框架,主要用来负责与客户端交互,业务逻辑的调用. SpringMVC是Spring家族中的一大组件,Spring整合SpringMV ...