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 ...
随机推荐
- 在Django运行安装mysqlclient和pymysql
推荐使用douban提供的pipy国内镜像服务,如果想手动指定源,可以在pip后面跟-i 来指定源. 下载mysqlclient为例: pip install mysqlclient -i http: ...
- 在Windows下配置svn服务端钩子程序
需求一,svn提交时必须填写log日志的需求 @echo off :: :: Stops commits that have empty log messages. :: @echo off set ...
- 在 Windows10 系统中安装 Homestead 本地开发环境
在 windows10 系统中安装 homestead 本地开发环境 在 windows10 环境下安装 homestead 开发环境,网上有很多相关教程其中大多都是 mac 环境,很多大神都是用户的 ...
- 20190226-SecureCRT连接linux显示中文乱码
SecureCRT连接我的Ubuntu14时中文显示乱码 解决办法: 在session options里选择UTF-8
- Cache index coloring for virtual-address dynamic allocators
A method for managing a memory, including obtaining a number of indices and a cache line size of a c ...
- Linux C++服务器端进程SDK
目标:开发人员在这个SDK的基础上开发,不用关注技术细节,只需要开发对应业务消息处理函数即可 1.每个进程SDK包括一个系统服务和多个业务服务,对外发布服务ID,每个服务有一个请求队列和一个反馈队列, ...
- 【推荐】你必须知道的EF知识和经验
阅读目录 推荐MiniProfiler插件 数据准备 foreach循环的陷进 AutoMapper工具 联表查询统计 性能提升之AsNonUnicode 性能提升之AsNoTracking 多字 ...
- BZOJ 2818 Gcd (莫比乌斯反演 或 欧拉函数)
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Submit: 2534 Solved: 1129 [Submit][Status][Discu ...
- 【iOS开发系列】XIB IBOutlets use strong or weak ?
有人问.在ARC下,IBOutlets究竟应该定义成strong 还是 weak ?支持这个答案的人最多.答案仅是摘自官方文档的一个片段: From a practical perspective, ...
- Maven + SpringMVC + Mybatis
使用IDEA配置Maven + SpringMVC + Mybatis [一步一步踩坑详细配置完成] PS:初学,想使用Maven配置一个SpringMVC的开发环境,照着网上的各种图文解说,配置了好 ...