CF834D The Bakery
题目链接:戳我
题意:将一个长度为n的序列分为k段,使得总价值最大。一段区间的价值表示为区间内不同数字的个数
\(n<=35000,k<=50\)
开始想的转移方程是这个样子的——\(dp[i][j]\)表示前i个,分成j组,最大收益
然后转移方程为\(dp[i][j]=max(dp[i-1][j]+cur,dp[i-1][j-1]+1)\),其中cur表示这个数是否在当前组中出现过,判断可以用set来搞。
但是——不对!!!!
原因是同一种最大收益可能有不同的分组方式,而不同的分组方式显然具有后效性,不能DPqwqwq
所以我们更改DP方程——
\(dp[i][j]=max(dp[k][j-1]+calc(k+1,j))\)
\(dp[i][j]\)表示前i个数分成j份。
但是这个样子的话复杂度是\(O(n^2k)\)的,显然。。。。很凉凉。
于是我们考虑一个神奇的做法——
从j开始遍历(即把j放成外层循环),那么。。。我们遍历i的时候就是从头开始依次加入数,并计算了。而每次加入一个数对于calc的计算来说,就是对它上次出现的位置到现在这个位置都+1.但是注意如果本身就是第一个出现的,那么pre也要改一改,改成自己的。(不过我的代码直接整体前移了一位。)
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 350010
using namespace std;
int n,k;
int a[MAXN],dp[MAXN][55],pre[MAXN],f[MAXN],pos[MAXN];
struct Node{int x,l,r,sum,tag;}t[MAXN<<2];
inline int ls(int x){return x<<1;}
inline int rs(int x){return x<<1|1;}
inline void push_up(int x){t[x].sum=max(t[ls(x)].sum,t[rs(x)].sum);}
inline void solve(int x,int k)
{
t[x].sum+=k;
t[x].tag+=k;
}
inline void build(int x,int l,int r)
{
t[x].l=l,t[x].r=r;t[x].tag=0;
if(l==r) {t[x].sum=f[l];return;}
int mid=(l+r)>>1;
build(ls(x),l,mid);
build(rs(x),mid+1,r);
push_up(x);
}
inline void push_down(int x)
{
if(t[x].tag)
{
solve(ls(x),t[x].tag);
solve(rs(x),t[x].tag);
t[x].tag=0;
}
}
inline void update(int x,int ll,int rr)
{
int l=t[x].l,r=t[x].r;
if(ll<=l&&r<=rr) {solve(x,1);return;}
int mid=(l+r)>>1;
push_down(x);
if(ll<=mid) update(ls(x),ll,rr);
if(mid<rr) update(rs(x),ll,rr);
push_up(x);
}
inline int query(int x,int ll,int rr)
{
int l=t[x].l,r=t[x].r;
if(ll<=l&&r<=rr) return t[x].sum;
int mid=(l+r)>>1,cur_ans=0;
push_down(x);
if(ll<=mid) cur_ans=max(cur_ans,query(ls(x),ll,rr));
if(mid<rr) cur_ans=max(cur_ans,query(rs(x),ll,rr));
return cur_ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
pre[i]=pos[a[i]];
pos[a[i]]=i;
}
//for(int i=1;i<=n;i++) printf("lst[%d]=%d\n",i,pre[i]);
for(int j=1;j<=k;j++)
{
build(1,0,n-1);
for(int i=1;i<=n;i++)
update(1,pre[i],i-1),f[i]=query(1,0,i-1);
}
printf("%d\n",f[n]);
return 0;
}
CF834D The Bakery的更多相关文章
- Codeforeces 707B Bakery(BFS)
B. Bakery time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- 信号量和PV操作写出Bakery算法的同步程序
面包店烹制面包及蛋糕,由n个销售员卖出.当有顾客进店购买面包或蛋糕时,应先在取号机上取号,然后等待叫号,若有销售员空闲时便叫下一号,试用信号量和PV操作写出Bakery算法的同步程序. 设计要求 1) ...
- 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 ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- 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 Round #368 (Div. 2) B. Bakery 水题
B. Bakery 题目连接: http://www.codeforces.com/contest/707/problem/B Description Masha wants to open her ...
- 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 ...
- CF833B The Bakery 线段树,DP
CF833B The Bakery LG传送门 线段树优化DP. 其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题. 先考虑\(O(n^2k)\)做法:设\(f[i][ ...
随机推荐
- docker 基本使用和安装提速
https://www.cnblogs.com/Erik_Xu/p/6662936.html#redis >yum install -y docker 道客提速 先安装curl >yum ...
- 实用 Linux 命令行使用技巧集锦
最近在Quora上看到一个问答题目,关于在高效率Linux用户节省时间Tips.将该题目的回答进行学习总结,加上自己的一些经验,记录如下,方便自己和大家参考. 下面介绍的都是一些命令行工具,这些工具在 ...
- Halcon中二维码解析函数解码率和时长的优化方法
Halcon中条码解析函数包容多种条码类型且简单强大.现有的‘Data Matrix ECC 200’.‘QR Code’和‘PDF417’等广泛使用的条码均能解析.简单是通过默认参数即可对多种条码进 ...
- 迷你MVVM框架 avalonjs 1.2.4发布
这段时间一直忙于建立avalon的单元测试,与重构官网.对avalon的更新都是来自公司内部的需求,性能优化与一些BUG修复. 添加大量调试日志. 重构shimController,以提高性能. cr ...
- Centos7手动编译安装Python3
Python3的安装方式非常的简单,首先去到Python下载目录下载想要的Python包,这里我选择了3.6.5版本 $ wget https://www.python.org/ftp/python/ ...
- java反射之ClassLoader
类加载器ClassLoader ClassLoader能在运行时, 知道任意一个类的的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性. 一.类加载器的工作机制 1:加载Jvm把clas ...
- Kafka管理工具介绍
Kafka内部提供了许多管理脚本,这些脚本都放在$KAFKA_HOME/bin目录下,而这些类的实现都是放在源码的kafka/core/src/main/scala/kafka/tools/路径下. ...
- 39-python 字符串替换+正则
from bs4 import BeautifulSoup import urllib.request import re moduel =re.compile('<.*?>') st = ...
- C语言压缩/解压缩
一.简介 Lzlib 压缩库提供了在内存中的 LZMA 压缩和解压算法功能,包括对数据进行完整性检查.压缩格式是 lzip 参考: http://blog.csdn.net/damenhanter/a ...
- Linux select/poll和epoll实现机制对比
关于这个话题,网上已经介绍的比较多,这里只是以流程图形式做一个简单明了的对比,方便区分. 一.select/poll实现机制 特点: 1.select/poll每次都需要重复传递全部的监听fd进来,涉 ...