题目描述
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 toany of the trees no. , 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 leastas 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 leasttiresome, 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 getfrom 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!
从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制,求每次最少耗费多少体力
输入输出格式
输入格式:There is a single integer () in the first line of the standard input:
the number of trees in the Byteotian Line Forest.
The second line of input holds integers ()separated by single spaces: is the height of the -th tree.
The third line of the input holds a single integer (): the number of birds whoseflights need to be planned.
The following lines describe these birds: in the -th of these lines, there is an integ…
输出格式:Your program should print exactly lines to the standard output.
In the -th line, it should specify the minimum number of tiresome flight legs of the -th bird.
输入输出样例
9 4 6 3 6 3 7 2 6 5 2 2 5
2 1
说明
从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制,求每次最少耗费多少体力
思路:
注意,如果对于两个位置j1和j2,有f[j1]<f[j2],则j1一定比j2更优。因为就算j1高度比较矮,到达i顶多再多消耗1个疲劳值,顶多和j2相等。
如果不需要消耗疲劳值,比j2更优。 如果f[j1]=f[j2],则我们比较它们的高度D,高度高的更优。
上代码:
#include<cstdio> #include<iostream> using namespace std; const int M = 1000001; int n,m,h[M],dp[M],que[M],ask; int main() { scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&h[i]); scanf("%d",&m); while(m--) { scanf("%d",&ask); int l=0,r=0; que[r]=1; dp[1]=0; for(int i=2; i<=n; i++) { while(l<=r && (que[l]+ask<i)) l++; if(h[que[l]]>h[i]) dp[i]=dp[que[l]]; else dp[i]=dp[que[l]]+1; while(l<=r && (dp[que[r]]>dp[i] || dp[que[r]]==dp[i]&&h[que[r]]<h[i]) ) r--; que[++r]=i; } printf("%d\n",dp[n]); } return 0; }