Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- void maxElement(int *arr,int n,int k){
- deque<int> q;
- int i=0;
- for(i=0;i<k;i++){
- while(!q.empty() && arr[i]>=arr[q.back()]){
- q.pop_back();
- }
- q.push_back(i);
- }
- while(i<n){
- cout << arr[q.front()] << " ";
- while(!q.empty() && q.front() <= i-k){
- q.pop_front();
- }
- while(!q.empty() && arr[i]>=arr[q.back()]){
- q.pop_back();
- }
- q.push_back(i);
- i++;
- }
- cout << arr[q.front()] << endl;
- }
- int main() {
- #ifndef ONLINE_JUDGE
- // for getting input from input.txt
- freopen("input.txt", "r", stdin);
- // for writing output to output.txt
- freopen("output.txt", "w", stdout);
- #endif
- int n,k;
- cin >> n >> k;
- int arr[n];
- for(int i=0;i<n;i++){
- cin >> arr[i];
- }
- maxElement(arr,n,k);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement