标签:
void solve() { int ans=0,i,j,front1,front2,rear1,rear2; front1=rear1=0; front2=rear2=0; for(i=1,j=1;j<=n;j++) { while(rear1>front1&&d[qmax[rear1-1]]<=d[j]) rear1--; qmax[rear1++]=j; while(rear2>front2&&d[qmin[rear2-1]]>=d[j]) rear2--; qmin[rear2++]=j; if(d[qmax[front1]]-d[qmin[front2]]>m) { ans=max(ans,j-i); while(d[qmax[front1]]-d[qmin[front2]]>m) { i=min(qmax[front1],qmin[front2])+1; while(rear1>front1&&qmax[front1]<i) front1++; while(rear2>front2&&qmin[front2]<i) front2++; } } } ans=max(ans,j-i); printf("%d\n",ans); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%d", &d[i]); solve(); return 0; }
标签:
原文地址:http://www.cnblogs.com/Rojo/p/4701621.html