Loading... #### 单调栈 ```cpp #include<bits/stdc++.h> using namespace std; const int N = 100; int stk[N], arr[N]; int n, t; int main(){ cin >> n; for(int i = 0; i < n; i++){ cin >> arr[i]; while(t && arr[i] <= stk[t]) t--; if(t) cout << stk[t] << " "; else cout << -1 << " "; stk[++t] = arr[i]; } return 0; } ``` #### 单调队列 ```cpp #include<bits/stdc++.h> using namespace std; const int N = 100; int arr[N], q[N]; int n, k; int main(){ scanf("%d %d", &n, &k); for(int i = 0; i < n; i++) scanf("%d", &arr[i]); int h = 0, r = -1; for(int i = 0; i < n; i++){ if(h <= r && i - k + 1 > q[h]) h++; while(h <= r && arr[q[r]] >= arr[i]) r--; q[++r] = i; if(i >= k - 1) printf("%d ", arr[q[h]]); } return 0; } ``` Last modification:January 31, 2023 © Allow specification reprint Like 0 如果觉得我的文章对你有用,请随意赞赏
Comment here is closed