You are viewing a single comment's thread. Return to all comments →
using namespace std;
void printKMax(int arr[], int n, int k){ deque deq; int i=0; deque::iterator it; for (i = 0; i < k; ++i) { while (!deq.empty() && arr[i] >= arr[deq.back()]) { deq.pop_back(); } deq.push_back(i); } for (; i < n; ++i) { cout << arr[deq.front()] << " "; while (!deq.empty() && deq.front() <= i - k) { deq.pop_front(); } while (!deq.empty() && arr[i] >= arr[deq.back()]) { deq.pop_back(); } deq.push_back(i); } cout << arr[deq.front()] << endl; }
int main(){
int t; cin >> t; while(t--) { int n,k; cin >> n >> k; int i; int arr[n]; for(i=0;i<n;i++) cin >> arr[i]; printKMax(arr, n, k); } return 0;
}
Seems like cookies are disabled on this browser, please enable them to open this website
Deque-STL
You are viewing a single comment's thread. Return to all comments →
include
include
using namespace std;
void printKMax(int arr[], int n, int k){ deque deq; int i=0; deque::iterator it; for (i = 0; i < k; ++i) { while (!deq.empty() && arr[i] >= arr[deq.back()]) { deq.pop_back(); } deq.push_back(i); } for (; i < n; ++i) { cout << arr[deq.front()] << " "; while (!deq.empty() && deq.front() <= i - k) { deq.pop_front(); } while (!deq.empty() && arr[i] >= arr[deq.back()]) { deq.pop_back(); } deq.push_back(i); } cout << arr[deq.front()] << endl; }
int main(){
}