fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6. ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  7. //freopen("input.txt","r",stdin);
  8. int nTest; cin >> nTest;
  9. while(nTest--){
  10. int n, k; cin >> n >> k;
  11. vector<int> a(n);
  12. for(int i=0; i<n; i++) cin >> a[i];
  13. vector<int> r(n);
  14. for(int i=n-1; i>=0; i--){
  15. r[i] = i;
  16. while (r[i]<n-1 && a[r[i] + 1]>=a[i]) r[i] = r[r[i] + 1];
  17. }
  18. vector<int> ans(n);
  19. deque<int> dq;
  20. for(int i=0; i<n; i++){
  21. while (dq.size() && (a[dq.back()]>a[i] || r[dq.back()] < i || dq.back()<i-k+1)) dq.pop_back();
  22. dq.push_front(i);
  23. ans[i] = a[dq.back()];
  24. }
  25. for(int i=k-1; i<n; i++) cout << ans[i] << " ";
  26. cout << '\n';
  27. }
  28. return 0;
  29. }
Success #stdin #stdout 0s 4856KB
stdin
2
4 2
3 2 4 1
3 3
1 2 3
stdout
2 2 1 
1