题目链接

题意分析

首先这道题不可以使用简单的贪心来做

根据\(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\)的形式

所以我们可以使用单调队列优化


\[sum[i]≥sum[j]+pre[j]
\]

根据贪心法则 我们希望恰好满足

所以我们维护一个递增的单调队列

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 合并神犇的更多相关文章

  1. 洛谷 P2300 合并神犇 解题报告

    P2300 合并神犇 题目背景 loidc来到了NOI的赛场上,他在那里看到了好多神犇. 题目描述 神犇们现在正排成一排在刷题.每个神犇都有一个能力值p[i].loidc认为坐在附近的金牌爷能力参差不 ...

  2. DP——P2300 合并神犇

    题目背景 loidc来到了NOI的赛场上,他在那里看到了好多神犇. 题目描述 神犇们现在正排成一排在刷题.每个神犇都有一个能力值p[i].loidc认为坐在附近的金牌爷能力参差不齐非常难受.于是loi ...

  3. 洛谷P2300 合并神犇

    传送门啦 分析: 刚开始读完题后感觉很懵,怎么算都不是3,结果发现题目理解错了.题目要求的是求一个不降的序列,不是递减的(发现自己好傻) 看明白题就好做了吧.经典的区间dp题,合并果子大家应该都做过, ...

  4. 洛谷 P2300 合并神犇

    洛谷 听说这题可以\(n^2\)水过去,不过这里介绍一种\(O(n)\)的做法. \(f[i]\)为第\(1-i\)位合并的次数. \(pre[i]\)为第\(1-i\)位最末尾的数. \(j\)为满 ...

  5. [Luogu2600]合并神犇(dp,贪心)

    [Luogu2600]合并神犇 题目背景 loidc来到了NOI的赛场上,他在那里看到了好多神犇. 题目描述 神犇们现在正排成一排在刷题.每个神犇都有一个能力值p[i].loidc认为坐在附近的金牌爷 ...

  6. 【BZOJ4916】神犇和蒟蒻(杜教筛)

    [BZOJ4916]神犇和蒟蒻(杜教筛) 题面 BZOJ 求 \[\sum_{i=1}^n\mu(i^2)\ \ 和\ \sum_{i=1}^n\phi(i^2)\] 其中\[n<=10^9\] ...

  7. [BZOJ 4916]神犇和蒟蒻

    Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; Output 请你 ...

  8. 【刷题】BZOJ 4916 神犇和蒟蒻

    Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; Output 请你 ...

  9. BZOJ4916: 神犇和蒟蒻【杜教筛】

    Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; Output 请你 ...

随机推荐

  1. Jenkins + Jmeter +Ant自动化集成环境搭建(一)

    所需工具 一.jmeter 工具下载 https://jmeter.apache.org/  配置环境JDK等及各种插件可以看小七之前的教程 二.Ant安装(http://ant.apache.org ...

  2. spark reduceByKey

    reduce(binary_function) reduce将RDD中元素前两个传给输入函数,产生一个新的return值,新产生的return值与RDD中下一个元素(第三个元素)组成两个元素,再被传给 ...

  3. Part2_lesson2---ARM处理器工作模式

    arm公司发布的学习手册:ARM Architecture Reference Manual. 打开之: 找到Programmers' Model->A2.2 Processor modes. ...

  4. 无废话 Thrift 之 Hello World( PHP 版).

    Thrift 不再介绍.体验一把 PHP 的Server , PHP 的Client. 0.安装,装备环境,不表,运行 thrift -version 看到版本就行. 1.写 HelloThrift. ...

  5. http post 方法传递参数的2种方式

       1.StringEntity try{ HttpPost httpPost = new HttpPost(url); //param参数,可以为param="key1=value1&a ...

  6. win7设置开机启动virtualBOX虚拟机

    如果常用VirtualBox虚拟机系统的话,设置随开机启动也是很方便的.不需要打开VirtualBox窗口,直接启动VirtualBox虚拟机系统就可以了. 设置开机自启动VirtualBox虚拟机系 ...

  7. CodeForces 474A Keyboard (水题)

    题意:给定一个键盘,然后一行字母,和一个字符,代表把那一行字母在键盘上左移还是右移一位. 析:没什么好说的,直接暴力就好. 代码如下: #include<bits/stdc++.h> us ...

  8. XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)

    Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly fou ...

  9. delphi sqlsever 实现存在则更新,不存在

    (' if exists(select * from NBcommission where Type='''+sType+'''and ItemNo='''+s+''' ) '+ ' begin'+ ...

  10. 纯分享scp协议如何工作

    scp协议是什么, wiki上说: Secure copy or SCP is a means of securely transferring computer files between a lo ...