日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【BZOJ 3831】【Poi2014】Little Bird(单调队列优化dp)

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【BZOJ 3831】【Poi2014】Little Bird(单调队列优化dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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!

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.

題目大意:

有一排n棵樹,第i棵樹的高度是Di。

MHY要從第一棵樹到第n棵樹去找他的妹子玩。

如果MHY在第i棵樹,那么他可以跳到第i+1,i+2,...,i+k棵樹。

如果MHY跳到一棵不矮于當前樹的樹,那么他的勞累值會+1,否則不會。

為了有體力和妹子玩,MHY要最小化勞累值。

解題報告:

?dp[i]表示從第一棵樹到第i棵樹所需的最小疲勞值。dp[i] = min(dp[j] + (s[i] > s[j]))

單調隊列中的元素主要考慮它的時效性和價值,時效性用來刪除隊頭,價值和時效性綜合考慮刪除隊尾。

單調隊列中的時效性是越靠后(在隊列中)越好,那么隊列中元素的價值是:疲勞值和樹高的綜合考慮。

注意,如果對于兩個位置j1和j2,有f[j1]<f[j2],則j1一定比j2更優。因為就算j1高度比較矮,到達i頂多再多消耗1個疲勞值,頂多和j2相等。如果不需要消耗疲勞值,比j2更優。 如果f[j1]=f[j2],則我們比較它們的高度D,高度高的更優。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 1e6 + 5; const ll INF = 0x3f3f3f3f; ll a[MAX],dp[MAX]; int n,q,k; deque<ll> dq; int main() {cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i);cin>>q;while(q--) {scanf("%d",&k);while(dq.size()) dq.pop_back();dq.push_back(1);dp[1] = 0;for(int i = 2; i<=n; i++) {dp[i] = INF;while(!dq.empty() && dq.front() < i-k) dq.pop_front();dp[i] = dp[dq.front()] + (a[i] >= a[dq.front()]);//注意這兩句的順序!! while(!dq.empty() && (dp[dq.back()] > dp[i] || (dp[dq.back()]==dp[i]&&a[dq.back()]<=a[i])))//加等號是為了節省時間吧?因為這樣dq里的元素就少了。 dq.pop_back();dq.push_back(i);}printf("%lld\n",dp[n]);}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【BZOJ 3831】【Poi2014】Little Bird(单调队列优化dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。