[关闭]
@xunuo 2017-01-19T10:06:11.000000Z 字数 2431 阅读 1035

Sliding Window-----(线段树区间求最值)


Time limit 12000 ms  Case time limit 5000 ms Memory limit65536 kB

数据结构

来源:
poj:poj 2823 Sliding Window
vjudge: k-Sliding Window


Description

An array of size n ≤ 106 is given to you. There is a sliding window 
of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Window position Minimum value Maximum value
[1 3 -1] -3 5 3 6 7 -1 3
1 [3 -1 -3] 5 3 6 7 -3 3
1 3 [-1 -3 5] 3 6 7 -3 5
1 3 -1 [-3 5 3] 6 7 -3 5
Your task is to determine the maximum and minimum values in the sliding window at each position. 

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

题意

输入的第一行包括两个数:n和d;
第二行包括n个数;
题目要求在这n个数中求出区间长度为d的区间内的最大值和最小值,其中第一行为最小值,第二行为最大值;

解题思路:

其实挂这个题的本意是想让我们多练习单调队列的吧!,然而我还是用线段树敲的@_@||,,,,唉!那就线段树吧!就只需要查询一个区间的最大值和最小值就好了。。。。。然后你就套了模板。。。。
交的时候记得要交C++而不是G++!!记得,,然而你斌不知道有什么区别!!!呵呵哒!
啊啊啊啊!!!我好烦呐!!!!!

完整代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. int p[1000010];
  6. struct node
  7. {
  8. int l,r,n1,n2;
  9. }tree[1000010*4];
  10. void build(int id,int l,int r)
  11. {
  12. int mid;
  13. tree[id].l=l;
  14. tree[id].r=r;
  15. mid=(l+r)/2;
  16. if(l==r)
  17. {
  18. tree[id].n1=p[mid];
  19. tree[id].n2=p[mid];
  20. return;
  21. }
  22. build(2*id,l,mid);
  23. build(2*id+1,mid+1,r);
  24. tree[id].n1=max(tree[2*id].n1,tree[2*id+1].n1);
  25. tree[id].n2=min(tree[2*id].n2,tree[2*id+1].n2);
  26. }
  27. int querymax(int id,int a,int b)//²éѯ
  28. {
  29. int mid;
  30. if(tree[id].l==a&&tree[id].r==b)
  31. return tree[id].n1;
  32. mid=(tree[id].l+tree[id].r)/2;
  33. if(b<=mid)//×ó
  34. return querymax(2*id,a,b);
  35. else if(a>mid)
  36. return querymax(2*id+1,a,b);
  37. else
  38. return max(querymax(2*id,a,mid),querymax(2*id+1,mid+1,b));
  39. }
  40. int querymin(int id,int a,int b)//²éѯ
  41. {
  42. int mid;
  43. if(tree[id].l==a&&tree[id].r==b)
  44. return tree[id].n2;
  45. mid=(tree[id].l+tree[id].r)/2;
  46. if(b<=mid)//×ó
  47. return querymin(2*id,a,b);
  48. else if(a>mid)
  49. return querymin(2*id+1,a,b);
  50. else
  51. return min(querymin(2*id,a,mid),querymin(2*id+1,mid+1,b));
  52. }
  53. int main()
  54. {
  55. int n,m;
  56. while(scanf("%d%d",&n,&m)!=EOF)
  57. {
  58. int maxn,minn;
  59. for(int i=1;i<=n;i++)
  60. scanf("%d",&p[i]);
  61. build(1,1,n);
  62. for(int i=1;i<=n-m+1;i++)
  63. {
  64. minn=querymin(1,i,i+m-1);
  65. if(i==1)
  66. printf("%d",minn);
  67. else
  68. printf(" %d",minn);
  69. }
  70. printf("\n");
  71. for(int i=1;i<=n-m+1;i++)
  72. {
  73. maxn=querymax(1,i,i+m-1);
  74. if(i==1)
  75. printf("%d",maxn);
  76. else
  77. printf(" %d",maxn);
  78. }
  79. printf("\n");
  80. }
  81. return 0;
  82. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注