#include <bits/stdc++.h>
//using int_max = 0x3f3f3f3f;
#define long_max 9223372036854775807;
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
using VI = vector<int>;
typedef unsigned long long ull;
const int MAXN = 1e6;
int t;
int n;
int tree[2000010];
int dp[2000010];
int a[2000010];
int main() {
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&tree[i]);
tree[0] = 1e9;
int q;
cin>>q;
//j j-1,j-2,j-3,j-4 j-k
while(q--){
int k;
cin>>k;
dp[1] = 0;
dp[0] = 0;
int head = 1;
int tail = 1;
a[head] = 1;
for(int i=2;i<=n;i++){
while(head <= tail && a[head] < i - k) head++;
dp[i] = dp[a[head]] + (tree[i] >= tree[a[head]]);
while(head <= tail && (dp[a[tail]] > dp[i] || (dp[a[tail]] == dp[i] && tree[i] >= tree[a[tail]]))) tail--;
a[++tail] = i;
}
printf("%d\n",dp[n]);
}
}
虽然想到了就是单调队列优化dp,但没想到就是再入队的时候判断一下是否更优
出队比较元素有俩个 ,一个是疲劳值,一个是高度,
1.队尾疲劳值大,直接出队,
2.队尾疲劳值和第 i 相同, 当高度低, 出队