题目大意:
  $n(n\le10^6)$个点排成一排,每个点有一个高度$h_i$,现在要从$1$号点跳到$n$号点,从$i$号点出发跳到的点$j$满足$i<j\le i+k$,若$h_j\ge h_i$则增加$1$的代价。给出$q(q\le25)$组询问,对于每次给出的$k$,求从$1$跳到$n$的最小代价。

思路:
  用$f_i$表示从$1$跳到$i$的最小代价,则一个显然的状态转移方程为$f_i=\min\{f_j+[h_i\ge h_j]\}$。然而这样是$O(n^2q)$的,显然会TLE。
  考虑队列优化。若当前要加入队列的点是$i$,队尾元素为$j$。若$f_i<f_j$或$j_i=f_j$且$h_i>=h_j$,用$i$转移一定更优,将$j$出队​。转移时将超过$k$范围内的元素出队,每次用队首元素转移即可。时间复杂度$O(nq)$。

 #include<queue>
#include<cstdio>
#include<cctype>
#include<climits>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=1e6+;
int h[N],f[N];
std::deque<int> q;
int main() {
const int n=getint();
for(register int i=;i<=n;i++) h[i]=getint();
for(register int m=getint();m;m--) {
const int k=getint();
q.clear();
q.push_back();
for(register int i=;i<=n;i++) {
while(!q.empty()&&i-q.front()>k) q.pop_front();
f[i]=f[q.front()]+(h[i]>=h[q.front()]);
while(!q.empty()&&(f[q.back()]>f[i]||(f[q.back()]==f[i]&&h[q.back()]<=h[i]))) q.pop_back();
q.push_back(i);
}
printf("%d\n",f[n]);
}
return ;
}

[POI2014]Little Bird的更多相关文章

  1. Bzoj 3831 [Poi2014]Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...

  2. bzoj3831 [Poi2014]Little Bird 单调队列优化dp

    3831: [Poi2014]Little Bird Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 505  Solved: 322[Submit][ ...

  3. P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird 一只鸟从1跳到n.从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制k,求每次最少耗费多少体力 很简短的题目哼. ...

  4. 【BZOJ3831】[Poi2014]Little Bird 单调队列

    [BZOJ3831][Poi2014]Little Bird Description In the Byteotian Line Forest there are   trees in a row. ...

  5. 洛谷P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...

  6. [luogu]P3572 [POI2014]PTA-Little Bird(单调队列)

    P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...

  7. 单调队列优化DP || [Poi2014]Little Bird || BZOJ 3831 || Luogu P3572

    题面:[POI2014]PTA-Little Bird 题解: N<=1e6 Q<=25F[i]表示到达第i棵树时需要消耗的最小体力值F[i]=min(F[i],F[j]+(D[j]> ...

  8. 题解 P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird 首先,这道题的暴力dp非常好写 就是枚举所有能转移到他的点,如果当前枚举到的位置的值大于 当前位置的话,\(f[i]=min(f[i],f ...

  9. 【bzoj3831】[Poi2014]Little Bird 单调队列优化dp

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826475.html 题目描述 In the Byteotian Line Forest there are   t ...

  10. BZOJ 3831: [Poi2014]Little Bird【动态规划】

    Description In the Byteotian Line Forest there are   trees in a row. On top of the first one, there ...

随机推荐

  1. APP测试工程师面试题:之一

    第六题主要流程:编写计划 → 测试用例 → 评审用例 → 执行用例 → 写BUG →测修复情况 → 上线

  2. Vim常用指令总结(持续更新中)

    1 模式变更 命令 说明 a(append)/i(insert) 普通模式→插入模式 : 普通模式→命令行模式 ESC或者Ctrl 插入模式→普通模式 R(Replace)/Insert两次 普通模式 ...

  3. Python——开篇之词

    我也断断续续的用Python挺长时间了.但是一直都没有系统的学习过Python.很多东西都是现用现学.这样感觉对Python的理解太浅,完完全全就是搬砖的. 因此,我专门找了一个比较完整的老男孩的Py ...

  4. 爬虫:Scrapy14 - Telnet 终端(Telnet Console)

    Scrapy 提供了内置的 Telnet 终端,以供检查,控制 Scrapy 运行的进程.Telnet 仅仅是一个运行在 Scrapy 进程中的普通 Python 终端.因此你可以在其中做任何是. T ...

  5. c++ 2.1 编译器何时创建默认构造函数

    我们通常会说当生命一个 class 时,如果我们不为该 class 指定一个 constructor,那么编译器会替我们实现一个 connstructor,那么这种说法一定对吗? 事实上,这是不对的. ...

  6. 基于MAC OS 操作系统安装、配置hadoop

    在Mac上安装Hadoop 对我这个之前从未接触过*nix的用户来说,使用命令行来做一系列的事情还是废了一番功夫.特写这个记录,以做备份. 获取Java 我的Mac运行的操作系统是OS X 10.7 ...

  7. 一文看懂Kafka消息格式的演变

    摘要 对于一个成熟的消息中间件而言,消息格式不仅关系到功能维度的扩展,还牵涉到性能维度的优化.随着Kafka的迅猛发展,其消息格式也在不断的升级改进,从0.8.x版本开始到现在的1.1.x版本,Kaf ...

  8. 《c程序设计语言》读书笔记-4.2-扩充atof函数

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  9. 《c程序设计语言》-2.9

    #include <stdio.h> /*int bitcount(unsigned x) { int b; for(b = 0;x != 0;x >>= 1) { if(x ...

  10. Codeforces Round #328 (Div. 2) C 数学

    C. The Big Race time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...