fork(8) download
  1. // Shashank Jain
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<algorithm>
  6.  
  7. #define LIM 100001
  8.  
  9. using namespace std;
  10.  
  11. int arr[LIM]; // Input Array
  12. int LR[LIM]; // maximum from Left to Right
  13. int RL[LIM]; // maximum from Right to left
  14. int max_val[LIM]; // number of subarrays(windows) will be n-k+1
  15.  
  16. int main()
  17. {
  18.  
  19. int n,w,i,k; // 'n' is number of elements in array and 'w' is Window's Size
  20. cin>>n>>w;
  21.  
  22. k=n-w+1; // 'K' is number of Windows
  23.  
  24. for(i=1;i<=n;i++)
  25. cin>>arr[i];
  26.  
  27. for(i=1;i<=n;i++) // for maximum Left to Right
  28. {
  29. if(i%w==1)
  30. LR[i]=arr[i];
  31. else
  32. LR[i]=max(LR[i-1],arr[i]);
  33.  
  34. }
  35.  
  36. for(i=n;i>=1;i--) // for maximum Right to Left
  37. {
  38. if(i%w==0 || i==n )
  39. RL[i]=arr[i];
  40. else
  41. RL[i]=max(RL[i+1],arr[i]);
  42. }
  43.  
  44. for(i=1;i<=k;i++) // maximum
  45. max_val[i]=max(RL[i],LR[i+w-1]);
  46.  
  47. for(i=1;i<=k;i++)
  48. cout<<max_val[i]<<" ";
  49.  
  50. cout<<endl;
  51. return 0;
  52. }
Success #stdin #stdout 0s 4416KB
stdin
10 3
1 -2 5 6 0 9 8 -1 2 0
stdout
5 6 6 9 9 9 8 2