当前位置:   article > 正文

洛谷 2032.扫描

洛谷 2032.扫描

思路:单调队列

就是一个板子题,直接上代码:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<vector>
  7. #include<algorithm>
  8. #include<stack>
  9. #include<queue>
  10. #include<deque>
  11. #include <iomanip>
  12. #include<sstream>
  13. #include<numeric>
  14. #include<map>
  15. #include<limits.h>
  16. #include<unordered_map>
  17. #include<set>
  18. #define int long long
  19. #define MAX 2000200
  20. #define inf 0x3f3f3f3f
  21. #define _for(i,a,b) for(int i=a;i<(b);i++)
  22. #define ALL(x) x.begin(),x.end()
  23. using namespace std;
  24. typedef pair<int, int> PII;
  25. int n, m, k;
  26. int counts=inf;
  27. int dx[] = { 0,1,0,-1};
  28. int dy[] = { 1,0,-1,0 };
  29. int q[MAX];
  30. int arr[MAX];
  31. int b[MAX];
  32. void get_max(int a[], int b[], int n, int qujian) {
  33. int front = 0;
  34. int rear = -1;
  35. for (int i = 0; i < n; i++) {
  36. if (front <= rear && q[front] + qujian <= i)front++;
  37. while (front <= rear && a[q[rear]] <= a[i])rear--;
  38. q[++rear] = i;
  39. if (i >= qujian - 1)
  40. b[i] = a[q[front]];
  41. }
  42. }
  43. signed main() {
  44. ios::sync_with_stdio(false);
  45. cin.tie(NULL); cout.tie(NULL);
  46. cin >> n >> k;
  47. for (int i = 0; i < n; i++) {
  48. cin >> arr[i];
  49. }
  50. get_max(arr, b, n, k);
  51. _for(i, k - 1, n)
  52. cout << b[i] << endl;
  53. return 0;
  54. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/996522
推荐阅读
相关标签
  

闽ICP备14008679号