[关闭]
@lychee123 2017-08-19T02:40:37.000000Z 字数 1058 阅读 1198

HDU 3530 Subsequence(两个双向队列分别维护最大最小值)

STL


题面链接

用last来记录最末尾一位的脚标

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #include<algorithm>
  5. using namespace std;
  6. int a[100005];
  7. int main()
  8. {
  9. int n,m,k,i;
  10. while(~scanf("%d%d%d",&n,&m,&k))
  11. {
  12. for(i=0;i<n;i++)
  13. scanf("%d",&a[i]);
  14. if(m>k)
  15. {
  16. printf("0\n");
  17. continue;
  18. }
  19. deque<int>q1;///维护最大值(从头到尾从大到小)
  20. deque<int>q2;///维护最小值
  21. int last=-1,ans=0;
  22. for(i=0;i<n;i++)
  23. {
  24. while(!q1.empty()&&a[i]>a[q1.back()])
  25. q1.pop_back();
  26. while(!q2.empty()&&a[i]<a[q2.back()])
  27. q2.pop_back();
  28. q1.push_back(i);
  29. q2.push_back(i);
  30. while(!q1.empty()&&a[q1.front()]-a[q2.front()]>k)
  31. {
  32. if(q1.front()>q2.front())
  33. {
  34. last=q2.front();
  35. q2.pop_front();
  36. }
  37. if(q1.front()<q2.front())
  38. {
  39. last=q1.front();
  40. q1.pop_front();
  41. }
  42. }
  43. if(!q1.empty()&&!q2.empty()&&a[q1.front()]-a[q2.front()]>=m)
  44. ans=max(ans,i-last);
  45. }
  46. printf("%d\n",ans);
  47. }
  48. return 0;
  49. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注