BZOJ 3831 POI2014 Litter Bird

dp[i]表示前i棵树的最小体力消耗值
但是如果直接上肯定时间复杂度会爆炸 (N*Q*K)
N和Q已经无法优化
所以需要优化k 通过一种数据结构找到 k个位置中最合适的位置 从而达到N*Q的时间复杂度
 
线段树和树状数组略有吃力,所以需要根据题目的单调性需要单调队列。
 
什么情况需要优化呢?
1.当前的位置dp值相等 但比之前的k个数的中某个数大 那肯定保留大的
2.如果dp值不相等 肯定保留小的那个位置
 
代码实现前仔细思考!!!!

 1 #include <cstdio>
 2 #include <queue>
 3 #include <algorithm>
 4 #include <cstring>
 5  
 6 inline int Read(){
 7     int x = 0,f = 1;
 8     char ch = getchar();
 9     while(ch < ‘0‘ || ch >‘9‘){
10         if(ch==‘-‘) f = -1;
11         ch = getchar();
12     }
13     while(ch >= ‘0‘ && ch <= ‘9‘){
14         x = x*10 + ch - ‘0‘;
15         ch = getchar();
16     }
17     return x*f;
18 }
19  
20 int dp[1000005],num[1000005];
21 int n,m;
22 std::deque<int>Q;
23  
24 int GetOut(int k){
25     memset(dp,0x3f3f3f3f,sizeof(dp));
26     Q.clear();
27     Q.push_back(1);
28     dp[1]=0;
29     for(int i=2;i<=n;i++){
30         //printf("fuck\n");
31         while(!Q.empty() && Q.front() < i - k) Q.pop_front();
32         dp[i] = dp[Q.front()] + (num[i] >= num[Q.front()]);
33         //printf("fuck\n");
34         while( !Q.empty() ){
35             if(dp[i] == dp[Q.back()]){
36                 if(num[i] > num[Q.back()]){
37                      Q.pop_back();
38                 }
39                 else break;
40             }
41             else{
42                 if(dp[i] < dp[Q.back()]){
43                     Q.pop_back();
44                 }
45                 else break;
46             }
47         }
48         Q.push_back(i);
49         //printf("%d\n",i);
50     }
51     return dp[n];
52 }
53  
54 void init(){
55     n = Read();
56     for(int i=1;i<=n;i++){
57         num[i] = Read(); 
58     }  
59     m = Read();
60     for(int i=1;i<=m;i++){
61         printf("%d\n",GetOut( Read() ));
62     }
63     return;
64 }
65  
66 int main(){
67     init();
68     return 0;
69 }
时间: 2024-08-24 01:12:23

BZOJ 3831 POI2014 Litter Bird的相关文章

bzoj 3831: [Poi2014]Little Bird

3831: [Poi2014]Little Bird 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

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

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 with

单调队列应用--BZOJ 3831 Little Bird

3831: [Poi2014]Little Bird Time Limit: 20 Sec  Memory Limit: 128 MB 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

BZOJ 3831

3831: [Poi2014]Little Bird Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 121  Solved: 68[Submit][Status] 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

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

3831: [Poi2014]Little Bird Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 505  Solved: 322[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 t

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

[BZOJ3831][Poi2014]Little Bird 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

P3572 [POI2014]PTA-Little Bird

P3572 [POI2014]PTA-Little Bird 一只鸟从1跳到n.从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制k,求每次最少耗费多少体力 很简短的题目哼. 首先对于一个点, 他的状态一定是由前 \(k\) 个转移过来的. \(k\) 的长度在每组询问内一定, 想到用单调队列维护 \(dp\) . 不过此时单调队列里的元素有两个关键字: 劳累度和高度, 因为跳到比这个点高的树需要花费恒为一点体力(这个很重要), 所以我们维护单调队列的时候可以以劳累度为

[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 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 s

BZOJ 3831 POI 2014 Little Bird 单调队列DP

题目大意:给出一片树林,树排成一排,每一棵树都有一个高度.从地一棵树出发,每次可以跳到i+k棵之前,跳到小于自己高度的树上不需要花费体力,反之需要花费一点体力,问到最后一棵树最少需要多少体力. 思路:简单DP方程:f[i] = min{f[j] + (height[i] >= height[j])} 然后发现数据范围只有O(n)可以过. 维护单调队列,队列中按照f单调递减,队尾按照时间往出弹. 当f值相同的时候,高度较高的优先. CODE: #include <queue> #inclu