DP/单调队列优化


水题水题水题水题

单调队列优化的线性dp……

WA了8次QAQ,就因为我写队列是[l,r),但是实际操作取队尾元素的时候忘记了……不怎么从队尾取元素嘛……平时都是直接往进放的……还是得记住这个双端队列的错点啊!!

 //BZOJ 3831
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
const int N=,INF=~0u>>;
//#define debug
int n,m,a[N],f[N],q[N]; int main(){
#ifndef ONLINE_JUDGE
freopen("file.in","r",stdin);
#endif
scanf("%d",&n);
F(i,,n) scanf("%d",&a[i]); scanf("%d",&m);
while(m--){
int l=,r=,k=;
memset(q,,sizeof q); scanf("%d",&k);
f[]=f[]=;
q[r++]=;
F(i,,n){
while(q[l]+k<i && l<r) l++;
f[i]=f[q[l]] + ((a[q[l]]<=a[i]) ? : ) ;
while( ((f[i]<f[q[r-]]) || (f[i]==f[q[r-]] && a[i]>=a[q[r-]])) && l<r) r--;
q[r++]=i;
}
printf("%d\n",f[n]);
}
return ;
}

3831: [Poi2014]Little Bird

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 191  Solved: 114
[Submit][Status][Discuss]

Description

In
the Byteotian Line Forest there are   trees in a row. On top of the
first one, there is a little bird who would like to fly over to the top
of the last tree. Being in fact very little, the bird might lack the
strength to fly there without any stop. If the bird is sitting on top of
the tree no.  , then in a single flight leg it can fly to any of the
trees no.i+1,i+2…I+K, and then has to rest afterward.
Moreover, flying up is far harder to flying down. A flight leg is
tiresome if it ends in a tree at least as high as the one where is
started. Otherwise the flight leg is not tiresome.
The goal is to select the trees on which the little bird will land
so that the overall flight is least tiresome, i.e., it has the minimum
number of tiresome legs. We note that birds are social creatures, and
our bird has a few bird-friends who would also like to get from the
first tree to the last one. The stamina of all the birds varies, so the
bird's friends may have different values of the parameter  . Help all
the birds, little and big!
有一排n棵树,第i棵树的高度是Di。
MHY要从第一棵树到第n棵树去找他的妹子玩。
如果MHY在第i棵树,那么他可以跳到第i+1,i+2,...,i+k棵树。
如果MHY跳到一棵不矮于当前树的树,那么他的劳累值会+1,否则不会。
为了有体力和妹子玩,MHY要最小化劳累值。

Input

There is a single
integer N(2<=N<=1 000 000) in the first line of the standard
input: the number of trees in the Byteotian Line Forest. The second line
of input holds   integers D1,D2…Dn(1<=Di<=10^9) separated by
single spaces: Di is the height of the i-th tree.
The third line of the input holds a single integer
Q(1<=Q<=25): the number of birds whose flights need to be planned.
The following Q lines describe these birds: in the i-th of these lines,
there is an integer Ki(1<=Ki<=N-1) specifying the i-th bird's
stamina. In other words, the maximum number of trees that the i-th bird
can pass before it has to rest is Ki-1.

Output

Your program
should print exactly Q lines to the standard output. In the I-th line,
it should specify the minimum number of tiresome flight legs of the i-th
bird.

Sample Input

9
4 6 3 6 3 7 2 6 5
2
2
5

Sample Output

2
1

HINT

Explanation: The first bird may stop at the trees no. 1, 3, 5, 7, 8, 9. Its tiresome flight legs will be the one from the 3-rd tree to the 5-th one and from the 7-th to the 8-th.

Source

[Submit][Status][Discuss]

【BZOJ】【3831】【POI2014】Little Bird的更多相关文章

  1. 【BZOJ 2754 喵星球上的点名】

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2512  Solved: 1092[Submit][Status][Discuss] Descript ...

  2. 【BZOJ】3052: [wc2013]糖果公园

    http://www.lydsy.com/JudgeOnline/problem.php?id=3052 题意:n个带颜色的点(m种),q次询问,每次询问x到y的路径上sum{w[次数]*v[颜色]} ...

  3. 【BZOJ】3319: 黑白树

    http://www.lydsy.com/JudgeOnline/problem.php?id=3319 题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种: ...

  4. 【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3319 以为是模板题就复习了下hld............................. 然后n ...

  5. 【BZOJ】【1025】【SCOI2009】游戏

    DP/整数拆分 整个映射关系可以分解成几个循环(置换群的预备知识?),那么总行数就等于各个循环长度的最小公倍数+1(因为有个第一行的1~N).那么有多少种可能的排数就等于问有多少种可能的最小公倍数. ...

  6. 【BZOJ】1013: [JSOI2008]球形空间产生器sphere

    [BZOJ]1013: [JSOI2008]球形空间产生器sphere 题意:给n+1个n维的点的坐标,要你求出一个到这n+1个点距离相等的点的坐标: 思路:高斯消元即第i个点和第i+1个点处理出一个 ...

  7. 【BZOJ】1002:轮状病毒(基尔霍夫矩阵【附公式推导】或打表)

    Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图 ...

  8. 【BZOJ】【3697】采药人的路径&【3127】【USACO2013 Open】Yin and Yang

    点分治 Orz hzwer 倒是比较好想到点分治……然而在方案统计这里,我犯了两个错误…… 1.我比较傻逼的想的是:通过儿子来更新父亲,也就是统计以x为根的子树中xxxx的路径有多少条……这样转移. ...

  9. 【BZOJ】【3083】遥远的国度

    树链剖分/dfs序 其实过了[BZOJ][4034][HAOI2015]T2以后就好搞了…… 链修改+子树查询+换根 其实静态树的换根直接树链剖分就可以搞了…… 因为其实只有一样变了:子树 如果roo ...

  10. 【BZOJ】【2434】【NOI2011】阿狸的打字机

    AC自动机+DFS序+BIT 好题啊……orz PoPoQQQ 大爷 一道相似的题目:[BZOJ][3172][TJOI2013]单词 那道题也是在fail树上数有多少个点,只不过这题是在x的fail ...

随机推荐

  1. maraidb半同步复制

    半同步复制是在主从同步复制的基础之上,主从同步前面我的博文有介绍 Semisynchronous Replication(半同步复制)则一定程度上保证提交的事务已经传给了至少一个备库 MySQL在加载 ...

  2. spark-submit工具参数说明

    执行时需要传入的参数说明 Usage: spark-submit [options] <app jar | python file> [app options] 参数名称 含义 --mas ...

  3. asp.net导出excel示例代码

    asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> );             ;       ...

  4. 二,CentOS minimal 网络配置及用yum安装所需软件

    CentOS minimal在刚安装完成后,ifconfig一下没发现网卡,是因为使用最小安装的网卡默认没启动,设置配置文件很简单,如下: 1.打开配置文件 vi /etc/sysconfig/net ...

  5. 【Zend Studio】10.6.0版本设置默认编码为UTF-8

    1.打开Windows->Prefefences 2.找到Workspace->Text file encoding,修改为UTF-8,OK保存.

  6. centos彻底删除mysql

    yum remove mysql mysql-server mysql-libs compat-mysql51rm -rf /var/lib/mysqlrm /etc/my.cnf查看是否还有mysq ...

  7. Windows7下Microsoft Office Excel 不能访问文件解决方案

    1).开始--〉运行--〉cmd 2)命令提示符下面,输入mmc -32,打开32的控制台 3).文件菜单中,添加删除管理单元--〉组件服务 4).在"DCOM配置"中找到&quo ...

  8. java-testng-selenium优化

    由于项目中webui测试的需要,是用testng+selenium的方式,其中遇到过几个问题,记录下,方便以后查看 1.重复运行多次case 因为是selenium,所以有的时候需要运行多次,方法是写 ...

  9. linux kernel 0.11 head

    head的作用 注意:bootsect和setup汇编采用intel的汇编风格,而在head中,此时已经进入32位保护模式,汇编的采用的AT&T的汇编语言,编译器当然也就变成对应的编译和连接器 ...

  10. Maven系列--pom.xml 配置详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...