标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 297 Accepted Submission(s): 152
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stack> #include<queue> #include<stdlib.h> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=100000+5; int a[MAXN],q1[MAXN],q2[MAXN]; int kase,n,k; int main() { cin>>kase; while(kase--) { LL ans=0; int f1,f2,t1,t2,l1,l2; f1=f2=t1=t2=0; l1=l2=-1; scanf("%d %d",&n,&k); for(int i=0;i<n;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) { while(f1<t1 && a[q1[t1-1]]<=a[i]) t1--;//维护最大值队列(递减) q1[t1++]=i; while(f2<t2 && a[q2[t2-1]]>=a[i]) t2--;//维护最小值队列(递增) q2[t2++]=i; while(a[q1[f1]] - a[q2[f2]]>=k)//差值过大 q1[f1]<q2[f2] ? l1=q1[f1++] : l2=q2[f2++]; ans+=i-max(l1,l2); } printf("%lld\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/clliff/p/4665756.html