We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
using namespace std;
void print(vector &arr, int k) {
deque dq;
int n = arr.size();
for (int i = 0; i < n; ++i) {
if (!dq.empty() && dq.front() == i - k)
dq.pop_front();
while (!dq.empty() && arr[dq.back()] < arr[i])
dq.pop_back();
dq.push_back(i);
if (i >= k - 1)
cout << arr[dq.front()] << " ";
}
cout << '\n';
}
int main()
{
// freopen("input.inp", "r", stdin);
// freopen("output.out", "w", stdout);
int t; cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector arr(n);
for (int i = 0; i < n; ++i)
cin >> arr[i];
print(arr, k);
}
return 0;
}
Cookie support is required to access HackerRank
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
using namespace std; void print(vector &arr, int k) { deque dq; int n = arr.size();
}
int main() { // freopen("input.inp", "r", stdin); // freopen("output.out", "w", stdout); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector arr(n); for (int i = 0; i < n; ++i) cin >> arr[i]; print(arr, k); } return 0; }