P2300 合并神犇
题目链接
题意分析
首先这道题不可以使用简单的贪心来做
根据\(DP\) 我们令\(dp[i]\)表示当前到了\(i\)一共做了\(dp[i]\)次合并
\(pre[i]\)表示当前合并到了\(i\)后序列末尾的数
那么$$dp[i]=min{dp[j]+i-j,sum[i]-sum[j]≥pre[j]}$$
可惜是\(O(n^2)\)的
我们考虑由于是\(dp[i]=dp[j]+val_i\)的形式
所以我们可以使用单调队列优化
\]
根据贪心法则 我们希望恰好满足
所以我们维护一个递增的单调队列
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>
#define ll long long
#define inf 0x7fffffff
#define N 500008
#define IL inline
#define M 1008611
#define D double
#define ull unsigned long long
#define R register
using namespace std;
template<typename T>IL void read(T &_)
{
T __=0,___=1;char ____=getchar();
while(!isdigit(____)) {if(____=='-') ___=0;____=getchar();}
while(isdigit(____)) {__=(__<<1)+(__<<3)+____-'0';____=getchar();}
_=___ ? __:-__;
}
/*-------------OI使我快乐-------------*/
int n,head,tail;
int num[N],que[N],dp[N];
ll sum[N],pre[N];
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
read(n);
for(R int i=1;i<=n;++i) read(num[i]);
for(R int i=1;i<=n;++i) sum[i]=sum[i-1]+1ll*num[i];
head=0;tail=1;
for(R int i=1;i<=n;++i)
{
while(head+1<tail&&sum[i]>=sum[que[head+1]]+pre[que[head+1]]) ++head;
pre[i]=sum[i]-sum[que[head]];dp[i]=i-que[head]-1+dp[que[head]];
while(head<tail&&sum[i]+pre[i]<sum[que[tail-1]]+pre[que[tail-1]]) --tail;
que[tail++]=i;
}
printf("%d\n",dp[n]);
// fclose(stdin);
// fclose(stdout);
return 0;
}
HEOI 2019 RP++
P2300 合并神犇的更多相关文章
- 洛谷 P2300 合并神犇 解题报告
P2300 合并神犇 题目背景 loidc来到了NOI的赛场上,他在那里看到了好多神犇. 题目描述 神犇们现在正排成一排在刷题.每个神犇都有一个能力值p[i].loidc认为坐在附近的金牌爷能力参差不 ...
- DP——P2300 合并神犇
题目背景 loidc来到了NOI的赛场上,他在那里看到了好多神犇. 题目描述 神犇们现在正排成一排在刷题.每个神犇都有一个能力值p[i].loidc认为坐在附近的金牌爷能力参差不齐非常难受.于是loi ...
- 洛谷P2300 合并神犇
传送门啦 分析: 刚开始读完题后感觉很懵,怎么算都不是3,结果发现题目理解错了.题目要求的是求一个不降的序列,不是递减的(发现自己好傻) 看明白题就好做了吧.经典的区间dp题,合并果子大家应该都做过, ...
- 洛谷 P2300 合并神犇
洛谷 听说这题可以\(n^2\)水过去,不过这里介绍一种\(O(n)\)的做法. \(f[i]\)为第\(1-i\)位合并的次数. \(pre[i]\)为第\(1-i\)位最末尾的数. \(j\)为满 ...
- [Luogu2600]合并神犇(dp,贪心)
[Luogu2600]合并神犇 题目背景 loidc来到了NOI的赛场上,他在那里看到了好多神犇. 题目描述 神犇们现在正排成一排在刷题.每个神犇都有一个能力值p[i].loidc认为坐在附近的金牌爷 ...
- 【BZOJ4916】神犇和蒟蒻(杜教筛)
[BZOJ4916]神犇和蒟蒻(杜教筛) 题面 BZOJ 求 \[\sum_{i=1}^n\mu(i^2)\ \ 和\ \sum_{i=1}^n\phi(i^2)\] 其中\[n<=10^9\] ...
- [BZOJ 4916]神犇和蒟蒻
Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; Output 请你 ...
- 【刷题】BZOJ 4916 神犇和蒟蒻
Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; Output 请你 ...
- BZOJ4916: 神犇和蒟蒻【杜教筛】
Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; Output 请你 ...
随机推荐
- Opencv normalize
#include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...
- spring4-2-bean配置-7-Spring表达式语言SpEL
- ORACLE修改表空间方法
一.使用imp/exp.先导出源库,再创建新库把表空间创建好,然后再导入.(据说这样可以,前提是新的库里面不能有与源库相同名字的表空间.有待验证!) 二.使用脚本进行修改.据目前所了解,正长情况下需要 ...
- SqlSugar Asp.Net 高性能ORM框架
SqlSugar从去年到现在已经一年了,版本从1.0升到了现在的2.4.1 ,这是一个稳定版本 ,有数家公司已经项目上线,在这里我将SqlSugar的功能重新整理成一篇新的贴子,希望大家喜欢. 公司团 ...
- CNN和GAN 比较nice的介绍
CNN是个什么鬼:https://www.zhihu.com/question/52668301 就是这样GAN : https://zhuanlan.zhihu.com/p/27199954
- CentOS 7 更换 阿里云/清华大学 yum 软件源
阿里云参考:https://opsx.alibaba.com/mirror?lang=zh-CN 清华参考:https://mirrors.tuna.tsinghua.edu.cn/help/cent ...
- Oracle 11g 重建EM需要删除的对象
因为需求需要重建EM,重建时因为某些错误被迫停止,比如对象已存在.用户已经存在等,最终找出了创建必备的条件: 1.环境变量(Oracle和Grid在同一个用户下安装) ORACLE_HOME 要设为D ...
- Introduction MBG
文档地址 http://www.mybatis.org/generator/configreference/xmlconfig.html 源码地址 https://github.com/mybatis ...
- Exception (3) Java exception handling best practices
List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...
- How To Start Building Spatially Aware Apps With Google’s Project Tango
How To Start Building Spatially Aware Apps With Google’s Project Tango “Tango can enable a whole new ...