悬关求助:20pts
查看原帖
悬关求助:20pts
398310
hundunqidian楼主2023/8/2 14:21

AC #1 #3

个人查错感觉可能是单调队列部分的问题,但没有想出来怎么改……

#include<bits/stdc++.h>
using namespace std;
int const X=1e6+100;
int n,d[X],Q,k;
int f[X],q[X],h,t;
signed main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>d[i];
	cin>>Q;
	while(Q--){
		memset(f,0,sizeof(f));
		memset(q,0,sizeof(q));
		cin>>k;
		h=1; t=0; //清空 
		for(int i=2;i<=n;i++){
			while(h<=t&&(f[q[t]]>f[i-1] || (f[q[t]]==f[i-1] && d[q[t]]==d[i-1]))){
				t--;
			} 
			q[++t]=i-1;
			if(q[h]<i-k) h++;
			f[i]=f[q[h]]+(d[i]>=d[q[h]]);
		}
		cout<<f[n]<<'\n';
	}
	return 0;
} 
2023/8/2 14:21
加载中...