[单调队列优化DP]bzoj3831 - cool's Blog

[单调队列优化DP]bzoj3831

cool posted @ 2015年12月01日 20:51 in DP , 573 阅读

3831: [Poi2014]Little Bird

Description

 

有一排n棵树,第i棵树的高度是Di。
JHT要从第一棵树到第n棵树去找他的妹子玩。
如果JHT在第i棵树,那么他可以跳到第i+1,i+2,...,i+k棵树。
如果JHT跳到一棵不矮于当前树的树,那么他的劳累值会+1,否则不会。
为了有体力和妹子玩,JHT要最小化劳累值。
 

 

Input

 

第一行N。第二行Di。第三行JHT的妹子个数。第四个每个妹子的K。

 

Output

 

每个妹子所需的体力
 

 

首先这是一道权限题(权限号?XMCXMC!)

显然的算法是O(nk),f[i] = min(min(f[j]+1)(D[i]>=D[j]),min(f[j])(D[i]<D[j]))(i-j<=k)

不过我们可以发现,当i<j且f[i]>f[j],那么i就不可能被用于转移。

同理若i<j且f[i]=f[j],a[i]<=a[j],那么i也不会被用于转移。

所以可以用单调队列优化DP。

核心代码如下:

            if (i-q[l] > k) l++;           
            f[i] = a[i]>=a[q[l]]?f[q[l]]+1:f[q[l]];
            for (;r&&(f[i]<f[q[r]]||(f[i] == f[q[r]]&&a[i]>a[q[r]]));r--);
            q[++r] = i;

我的DP还是太弱辣(蒟蒻就该多做题!),还是要多向黈力学习!

+1 Blueprint 2024 说:
2023年2月13日 18:05

Board of School Education Announced the Board 11th class Blueprint. It is the responsibility of schools to conduct the examination at their own pace and decide the New Blueprint. +1 Blueprint 2024 Get the latest 1st Inter Blueprint 2024 and Download the Board 11th Blueprint on its official website. Blueprint 2024 for 11th Class and start preparation according to the exam New Blueprint 2024 to score in Board exam.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee