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][ ...
随机推荐
- 利用iWARP/RDMA解决以太网高延迟
导读:“iWARP能够带来超低延迟.”据介绍,RDMA,即远程直接内存访问提供了应用程序到应用程序的直接通信能力,这也就意味着,应用将跳过操作系统,实现远程内存应用程序的访问 关键词: iWARP 低 ...
- leetcode441
public class Solution { public int ArrangeCoins(int n) { //convert int to long to prevent integer ov ...
- leetcode190
public class Solution { public uint reverseBits(uint n) { var list = new List<uint>();//逆序的二进制 ...
- Git 软件开发过程
一.关于Git与Subversion的区别 二.目前我们用Subversion是怎么执行软件过程的 三.优势与缺点 架构 * Git:分布式,所有的teammates本地可以clone一份独立完整的仓 ...
- 跟我学算法-match-LSTM(向唐老师看齐)
对于match-lstm,将hi文本与输出的match-lstm(由si,hi,qi)组合重新输入到LSTM网络中,以端对端的操作理念. 参考的博客:https://blog.csdn.net/lad ...
- 记一次为gitlab启用CI的过程
问题描述: 在局域网内搭了了一个gitlab,最近有需求要用CI 那时我不在 ,他们尝试了一段时间的Jenkins,但是还没有成功,我说gitlab已经有这些功能了,不用那个.于是一个人搞起来了. 从 ...
- 理解C# 4 dynamic(4) – 让人惊艳的Clay(转)
作者:Justrun名字来自<阿甘正传>,是希望自己能够更更傻一点. link: http://www.cnblogs.com/JustRun1983/p/3529157.html 理 ...
- SpinBlur - 旋转模糊
[SpinBlur - 旋转模糊] Using the Spin Blur effect, you can rotate and blur the image around one or more p ...
- SpringAop及拦截器
一.Aop Aop,面向切面编程,提供了一种机制,在执行业务前后执行另外的代码. 切面编程包括切面(Aspect),连接点(Joinpoint).通知(Advice).切入点(Pointcut).引入 ...
- codeforces:818G Four Melodies分析
题目 题目大意是有一组自然数v1,...,vn,要求在其中找到四个非空子序列(从原来的自然数序列中挑选一部分数,并按原先后关系排序),这些子序列互不相交,且每个子序列中的前后元素的值要么差值的绝对值为 ...