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 ...
随机推荐
- Project Euler 14 Longest Collatz sequence
题意:对于任意一个数 N ,寻找在 100,0000 之内按照规则( N 为奇数 N = N * 3 + 1 ,N 为偶数 N = N / 2 ,直到 N = 1 时的步数 )步数的最大值 思路:记忆 ...
- 使用shell脚本定时备份web网站代码
#!/bin/bash ############### common file ################ #备份文件存放目录 WEBBACK_DIR="/data/backup/ba ...
- 第n个质数
//注:for循环之后第三个式子总会操作一遍. #include <iostream> using namespace std; int main() { int n; while (ci ...
- python简单post信息
最近学了点关于python的网络爬虫的知识,简单记录一下,这里主要用到了requests库和BeautifulSoup库 Requests is an elegant and simple HTTP ...
- TensorFlow实现LeNet5模型
# -*- coding: utf-8 -*-import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_ ...
- 原生js,一些小应用(逢10进一,生成V字,多个div抖动)
第一题:每隔10个div换一行.并且鼠标移入 改变opacity. <!DOCTYPE html> <html lang="en"> <head> ...
- web端实现图片放大切换显示预览
项目中会遇到多张图片点击放大显示原图,并且能够左右滑动切换显示图片的需求,这种效果主要通过js来实现,下面我介绍的主要是借助swiper.js来实现这个完整的功能, 点击“查看协议” => 图片 ...
- 【树形DP】 HDU 2196 Computer
题意:求节点间的最大距离 先DFS一次 记录下 每一节点的子树下的最大距离(DP[ u ] [ 0 ])和第二大距离(DP[ u ] [ 1 ]) 用DP[ v ] [ 2 ] 表示由v的父节点来的最 ...
- android继续探索Fresco
我们接着上文继续说,上篇博客中我们已经知道了Fresco怎么用,也知道了它的非常多属性.可是非常多时候xml文件是不能满足你的要求的.这就须要你在代码中动态的改变显示的内容,今天我们就来探索一下怎样在 ...
- MongoDB初探系列之四:MongoDB与Java共舞
因为版本号不同,可能API也有所不同.本次学习用的是3.0版本号. 1.使用的mongodb的jdbc驱动版本号为:mongo-java-driver-3.0.0.jar 2.本节仅仅是简介JDBC操 ...