纯板子题,入队时保证单调性,即单调栈,出队保证题目条件,本题即窗口长度k,在入队出队时都可以维护信息
const int maxm = 1e6+5; int buf[maxm], maxq[maxm], minq[maxm], ans1[maxm], ans2[maxm]; int main() { ios::sync_with_stdio(false), cin.tie(0); int n, k, l1 = 0, r1 = -1, l2 = 0, r2 = -1; cin >> n >> k; for(int i = 0; i < n; ++i) cin >> buf[i]; for(int i = 0; i < n; ++i) { while(l1 <= r1 && minq[l1] <= i-k) l1++; while(l2 <= r2 && maxq[l2] <= i-k) l2++; while(l1 <= r1 && buf[minq[r1]] >= buf[i]) r1--; minq[++r1] = i; while(l2 <= r2 && buf[maxq[r2]] <= buf[i]) r2--; maxq[++r2] = i; if(i >= k-1) ans1[i] = buf[minq[l1]], ans2[i] = buf[maxq[l2]]; } for(int i = k-1; i < n; ++i) { if(i!=k-1) cout << " "; cout << ans1[i]; } cout << "\n"; for(int i = k-1; i < n; ++i) { if(i!=k-1) cout << " "; cout << ans2[i]; } cout << "\n"; return 0; }
来源:https://www.cnblogs.com/GRedComeT/p/12233285.html