CF833B The Bakery (线段树+DP)
题目大意:给你一个序列(n<=35000),最多分不大于m块(m<=50),求每个块内不同元素的数量之和的最大值
考试的时候第一眼建图,没建出来,第二眼贪心 ,被自己hack掉了,又随手写了个dp方程,感觉可以用splay维护,发现有区间操作并可取,又发现这个方程只能用线段树维护,然后只剩40min了我没调完,而且线段树打错了......
定义f[i][j]表示以第i个数为这个块的结尾,已经分了j块的答案的最大值
很明显,sum表示不同元素数量
对于每个元素,记录一个表示数 i 上一次出现的位置,那么遍历到i的时候,能更新sum的位置只有
到i-1,线段树维护区间修改!
而最大,线段树维护区间最大值!
算好空间,建立M颗线段树即可
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define il inline
#define mod 905229641
#define N 35100
#define M 52
#define il inline
using namespace std;
//re
int n,m;
int a[N],lst[N];
int f[N][M];
struct Seg{
int ma[N<<],tag[N<<];
il void pushup(int rt){ma[rt]=max(ma[rt<<],ma[rt<<|]);}
il void pushdown(int rt){
if(tag[rt])
ma[rt<<]+=tag[rt],ma[rt<<|]+=tag[rt],
tag[rt<<]+=tag[rt],tag[rt<<|]+=tag[rt],tag[rt]=;}
void update(int L,int R,int l,int r,int rt,int w)
{
if(L>R) return;
if(L<=l&&r<=R){ma[rt]+=w;tag[rt]+=w;return;}
pushdown(rt);
int mid=(l+r)>>;
if(L<=mid) update(L,R,l,mid,rt<<,w);
if(R>mid) update(L,R,mid+,r,rt<<|,w);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L>R) return ;
if(L<=l&&r<=R) {return ma[rt];}
pushdown(rt);
int mid=(l+r)>>,ans=;
if(L<=mid) ans=max(query(L,R,l,mid,rt<<),ans);
if(R>mid) ans=max(query(L,R,mid+,r,rt<<|),ans);
pushup(rt);
return ans;
}
}s[M];
int main()
{
freopen("handsome.in","r",stdin);
freopen("handsome.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
s[j-].update(lst[a[i]],i-,,n,,),
f[i][j]=s[j-].query(,i-,,n,),
s[j].update(i,i,,n,,f[i][j]);
lst[a[i]]=i;
}
int ans=;
for(int i=;i<=m;i++)
ans=max(ans,f[n][i]);
printf("%d\n",ans);
return ;
}
CF833B The Bakery (线段树+DP)的更多相关文章
- CF833B The Bakery 线段树,DP
CF833B The Bakery LG传送门 线段树优化DP. 其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题. 先考虑\(O(n^2k)\)做法:设\(f[i][ ...
- codeforces#426(div1) B - The Bakery (线段树 + dp)
B. The Bakery Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...
- Codeforces.833B.The Bakery(线段树 DP)
题目链接 \(Description\) 有n个数,将其分为k段,每段的值为这一段的总共数字种类,问最大总值是多少 \(Solution\) DP,用\(f[i][j]\)表示当前在i 分成了j份(第 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)
B. 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 ...
- Codeforces Round #426 (Div. 2) D The Bakery(线段树 DP)
The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP
D. The Bakery Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...
随机推荐
- python 操作jenkins 常用api
Python-Jenkins 继续寻找,然后我在Jenkins官网上找到了Python-Jenkins API,仔细阅读后发现,它几乎涵盖了大部分Jenkins的操作,大大方便了我们在后台进行对Jen ...
- nyoj181-小明的难题
小明的难题时间限制:3000 ms | 内存限制:65535 KB难度:2描述课堂上小明学会了用计算机求出N的阶乘,回到家后就对妹妹炫耀起来.为了不让哥哥太自满,妹妹给小明出了个问题"既 ...
- 如何计算合适的InnoDB log file size
原文链接:http://www.mysqlperformanceblog.com/2008/11/21/how-to-calculate-a-good-innodb-log-file-size/ Pe ...
- elasticsearch(七)java 搜索功能Search Request的介绍与使用
目录 1,首先创建主搜索请求: 2,对主搜索请求进行参数设置 前端几节都是介绍的基于单个文档或着单个文档库的操作, 本节开始将介绍基于所有或指定的任何个数文档库的操作的api SearchReques ...
- 01springMVC入门
1 MVC模式回顾 Spring MVC是一种基于MVC的Web应用框架. MVC是一种设计模式,MVC在b/s系统下的应用: 执行步骤: 发出请求,请求到MVC当中的C,C接收请求后并不能 ...
- My SQL中show命令--MySQL中帮助查看
My SQL中show命令--MySQL中帮助查看 学习了:http://hahaxiao.techweb.com.cn/archives/477.html 在mysql命令界面内,输入help或者? ...
- BestCoder Round #75 King's Order dp:数位dp
King's Order Accepts: 381 Submissions: 1361 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...
- COCOS学习笔记--Cocod2dx内存管理(三)-Coco2d-x内存执行原理
通过上两篇博客.我们对Cocos引用计数和Ref类.PoolManager类以及AutoreleasePool类已有所了解,那么接下来就通过举栗子来进一步看看Coco2d-x内存执行原理是如何的. / ...
- 怎样使用 CCache 进行 cocos2d-x 编译加速
https://github.com/chukong/cocos-docs/blob/master/manual/framework/native/v3/ccache-speed-up/zh.md C ...
- Ubuntu14.04编译WebRTC For Android代码 2014-07-24
整整快一年没有写博客了.近期基于Google开源的WebRTC项目做了一款音视频聊天的即时通信项目,期间在下载WebRTC代码时就碰到了一些问题.在此以作记录,也希望可以帮助到正在下载编译WebRTC ...