【题解】CF#833 B-The Bakery
一个非常明显的 \(nk\) dp 状态 \(f[i][k]\) 表示以 \(i\) 为第 \(k\) 段的最后一个元素时所能获得的最大代价。转移的时候枚举上一段的最后一个元素 \(j\)更新状态即可。考虑如何优化这个过程?主要的时间消耗在两个部分:一个是确定一段区间的贡献,另一个是找到最大的值。
这两个都是可以使用线段树来维护的,一段区间的贡献我们可以扫描线,而最大值则直接线段树维护最大值。如何滚动反而好像是最难的……想了一会儿,因为显然 memset 不可接受,然而我们可以 \(O(n)\) 建树啊……简直对自己的zz无语惹~
#include <bits/stdc++.h>
using namespace std;
#define maxn 2000000
#define maxm 40000
int n, K, ans, a[maxn], rec[maxn], last[maxn];
int f[maxm][]; int read()
{
int x = , k = ;
char c; c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} struct Segament_Tree
{
int mx[maxn], mark[maxn]; void Push_Down(int p)
{
mark[p << ] += mark[p], mark[p << | ] += mark[p];
mx[p << ] += mark[p], mx[p << | ] += mark[p];
mark[p] = ;
} void Build(int p, int l, int r, int K)
{
if(l == r) { mark[p] = , mx[p] = f[l - ][K]; return; }
int mid = (l + r) >> ;
mark[p] = ;
Build(p << , l, mid, K), Build(p << | , mid + , r, K);
mx[p] = max(mx[p << ], mx[p << | ]);
} void Update(int p, int l, int r, int L, int R, int x)
{
if(L <= l && R >= r) { mx[p] += x, mark[p] += x; return; }
if(L > r || R < l) return;
int mid = (l + r) >> ;
Push_Down(p);
Update(p << , l, mid, L, R, x);
Update(p << | , mid + , r, L, R, x);
mx[p] = max(mx[p << ], mx[p << | ]);
} int Query(int p, int l, int r, int L, int R)
{
if(L <= l && R >= r) { return mx[p]; }
if(L > r || R < l) return ;
int mid = (l + r) >> ;
Push_Down(p);
return max(Query(p << , l, mid, L, R), Query(p << | , mid + , r, L, R));
}
}T[]; int main()
{
n = read(), K = read();
for(int i = ; i <= n; i ++)
{
a[i] = read();
last[i] = rec[a[i]], rec[a[i]] = i;
}
int now = , pre = ;
for(int j = ; j <= K; j ++)
{
for(int i = ; i <= n; i ++)
{
T[pre].Update(, , n, last[i] + , i, );
f[i][j] = T[pre].Query(, , n, , i);
}
T[now].Build(, , n, j);
swap(now, pre);
}
printf("%d\n", f[n][K]);
return ;
}
【题解】CF#833 B-The Bakery的更多相关文章
- CF 833 B. The Bakery
B. The Bakery http://codeforces.com/contest/833/problem/B 题意: 将一个长度为n的序列分成k份,每份的cost为不同的数的个数,求最大cost ...
- 竞赛题解 - CF Round #524 Div.2
CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)
还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...
- 竞赛题解 - [CF 1080D]Olya and magical square
Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...
- [题解] [CF 1250J] The Parade
题面 题目大意: 给定一个 \(n\) , 所有军人的数量均在 \([1, n]\) 给定 \(a_i\) 代表高度为 \(i\) 的军人的个数 你要将这些军人分成 \(k\) 行, 满足下面两个条件 ...
- 题解 CF 1372 B
题目 传送门 题意 给出 \(n\),输出 \(a\) ,\(b\) (\(0 < a \leq b < n\)),使\(a+b=n\)且 \(\operatorname{lcm}(a,b ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T4(模拟)
随便模拟下就过了qwq 然后忘了特判WA了QwQ #include <cstdio> #include <algorithm> #include <cstring> ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T3(贪心)
是一道水题 虽然看起来像是DP,但其实是贪心 扫一遍就A了 QwQ #include <cstdio> #include <algorithm> #include <cs ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T2(模拟)
题目要求很简单,做法很粗暴 直接扫一遍即可 注意结果会爆int #include <cstdio> #include <algorithm> #include <cstr ...
随机推荐
- PHP数组中插入元素
1. array_unshift()数组头插入新元素 $fruits = array('apple','pear','banana','orange'); array_unshift($fruits, ...
- git配置github链接
1.百度git官网-下载最新版git 2.一路默认下一步安装 3.打开 git bash here 命令行 4.注册github账号(用自己的邮箱就可以,不会英文可以用谷歌翻译)注册成功后建立项目 5 ...
- python里pickle模块
Pickle模块用于将复杂的文件转化为二进制的文件 pickle模块一般是在源代码里面含有较大的字典或者列表等复杂文件时,我们如果将文件直接写在源代码里面,这样会使得代码很冗余,并且源代码文件所占空间 ...
- lesson 22 by heart
lesson 22 by heart on end = continuously 连续不断地 know/learn sth by heart 记忆sth falter: speak hesitantl ...
- Oracle存储过程练习题
1.1.创建一个过程,能向dept表中添加一个新记录.(in参数) 创建过程 create or replace procedure insert_dept ( num_dept in number, ...
- Linux文件系统简介和软链接和硬链接的区别
Linux有着极其丰富的文件系统,大体可分为如下几类: 网络文件系统:如nfs.cifs等: 磁盘文件系统:如ext3.ext4等: 特殊文件系统:如prco.sysfs.ramfs.tmpfs等: ...
- python简单循环生成器
import time #循环生成器 def traversal_list(alist, i): while True: length = len(alist) i = i%(length) yiel ...
- 第一次接触FPGA至今,总结的宝贵经验
从大学时代第一次接触FPGA至今已有10多年的时间,至今记得当初第一次在EDA实验平台上完成数字秒表.抢答器.密码锁等实验时那个兴奋劲.当时由于没有接触到HDL硬件描述语言,设计都是在MAX+plus ...
- 如何在html中把一个图片或者表格覆盖在一张已有图片上的任意位置
如何在html中把一个图片或者表格覆盖在一张已有图片上的任意位置 <div style="position:relative;"> <img src=&quo ...
- java — 值传递和引用传递
在 Java 应用程序中永远不会传递对象,而只传递对象引用.因此是按引用传递对象.Java 应用程序按引用传递对象这一事实并不意味着 Java 应用程序按引用传递参数.参数可以是对象引用,而 Java ...