新手理解(可能有理解错误的地方,请指教,嘿嘿)
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<stack> #define M 100005 using namespace std; int a,n,m,k,i,p[M],mintail,minhead,maxtail,maxhead,ans,trmp,last,last2; struct PP{ int viluee; //值 (单词打错了不要建议) int markee; //标号 }maxque[M],minque[M]; int main(){ while(~scanf("%d%d%d",&n,&m,&k)) { //清零 ans=0; maxhead=0;maxtail=0;minhead=0;mintail=0; last=last2=0; memset(minque,0,sizeof(minque));memset(maxque,0,sizeof(maxque)); //输入 for(i=1;i<=n;i++) scanf("%d",&p[i]); for(i=1;i<=n;i++) { while(maxhead<=maxtail&&maxque[maxtail].viluee<p[i]) maxtail--;//维持最大 maxque[++maxtail].viluee=p[i];maxque[maxtail].markee=i; while(minhead<=mintail&&minque[mintail].viluee>p[i]) mintail--;//维持最小 minque[++mintail].viluee=p[i];minque[mintail].markee=i; while(maxque[maxhead].viluee-minque[minhead].viluee>k) { if(maxque[maxhead].markee<=minque[minhead].markee) last=maxque[maxhead++].markee; //last保存的最大值的标号j是当前最大值-最小最 >k else //的标号,而不是maxhead++后的标号,目的是让j后面 last2=minque[minhead++].markee; // 元素包括进(当前以i为队尾的最大元素个数) //特别容易搞错,气死我了 } if(maxque[maxhead].viluee-minque[minhead].viluee>=m) { ans=max(ans,i-max(last,last2)); //确保2个队列的元素都有 } } printf("%d\n",ans); } return 0; }