P1886 滑动窗口 /【模板】单调队列
https://www.luogu.com.cn/problem/P1886
思路
https://zhuanlan.zhihu.com/p/346354943
Code
https://www.luogu.com.cn/record/143623041
LL n, k; LL a[1000005]; deque<LL> maxd, mind; int main() { cin >> n >> k; for(int i=0; i<n; i++){ cin >> a[i]; } LL loop = n - k + 1; /* ---- handle min case ---- */ for(LL i=0; i<loop; i++){ // first loop, enque all k item if (i == 0){ for(LL j=0; j<k; j++){ while(mind.size()){ if (a[mind.back()] >= a[j]){ mind.pop_back(); } else { break; } } mind.push_back(j); } // other case, only care about new comer } else { if (mind.size() && i - mind.front() >= 1){ mind.pop_front(); } int j = i + k - 1; while(mind.size()){ if (a[mind.back()] >= a[j]){ mind.pop_back(); } else { break; } } mind.push_back(j); } // cout << "------------loop i = " << i << endl; // for (int n : mind) // cout << n << ' '; // cout << endl; cout << a[mind.front()] << " "; } cout << endl; /* ---- handle max case ---- */ for(LL i=0; i<loop; i++){ // first loop, enque all k item if (i == 0){ for(LL j=0; j<k; j++){ while(maxd.size()){ if (a[maxd.back()] <= a[j]){ maxd.pop_back(); } else { break; } } maxd.push_back(j); } // other case, only care about new comer } else { if (maxd.size() && i - maxd.front() >= 1){ maxd.pop_front(); } int j = i + k - 1; while(maxd.size()){ if (a[maxd.back()] <= a[j]){ maxd.pop_back(); } else { break; } } maxd.push_back(j); } // cout << "------------loop i = " << i << endl; // for (int n : maxd) // cout << n << ' '; // cout << endl; cout << a[maxd.front()] << " "; } cout << endl; return 0; }
标签:P1886,LL,mind,back,pop,队列,int,模板 From: https://www.cnblogs.com/lightsong/p/17977678