[关闭]
@lychee123 2017-08-19T03:52:27.000000Z 字数 640 阅读 965

Codeforces 251A(求升序数组任意三个数中里最大差值不超过k的方案数)

STL


题面链接

  1. #include<stdio.h>
  2. #include<queue>///一定要包含这个头文件
  3. #include<string>
  4. #include<string.h>
  5. #include<algorithm>
  6. #include<iostream>
  7. using namespace std;
  8. int a[100010];
  9. int main()
  10. {
  11. int n,i,j,x;
  12. while(~scanf("%d%d",&n,&x))
  13. {
  14. for(i=0;i<n;i++)
  15. scanf("%d",&a[i]);
  16. queue<int>q;///队列是先进先出
  17. long long cnt=0,ans=0;
  18. for(i=0;i<n;i++)
  19. {
  20. q.push(a[i]);
  21. cnt++;
  22. while((q.back()-q.front())>x)///q.front()是最先输入的那个,是在栈底的元素
  23. {
  24. q.pop();
  25. cnt--;
  26. }
  27. ans+=((cnt-1)*(cnt-2)/2);///目前进入的一定是要选的
  28. }
  29. cout<<ans<<endl;
  30. }
  31. return 0;
  32. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注