Pytest中引數化之CSV檔案實戰
阿新 • • 發佈:2021-08-12
單調佇列
一種需要 人工 根據一定條件, 來篩除一些佇列中已經沒有資格再混下去的元素的資料結構
張三原本在學校的 OI 隊伍, 但天天不思進取, 上課只會打小遊戲, 結果......
身為高二學長的他竟然連高一新晉的 OIer 都比不過, 於是他們學校的 yxf 老師就將他開除了隊伍, 因為 新晉OIer年紀比他小, 能力比他強, 張三的整體價值已經比不過新晉OIer了 QwQ
蒟蒻程式碼
#include <bits/stdc++.h> #define re register using namespace std; const int N=1e6+5; int n,k; int q[N]; // 儲存元素索引, 方便判斷出隊 int head,tail; int a[N]; void monotoneMin(){ // memset(q,0,sizeof(q)); head=1, tail=0; for(re int i=1;i<=n;i++){ while(head<=tail && a[q[tail]]>=a[i]) tail--; q[++tail]=i; while(head<=tail && q[head]+k<=i) head++; if(i>=k) cout<<a[q[head]]<<" "; } cout<<endl; } void monotoneMax(){ // memset(q,0,sizeof(q)); head=1, tail=0; for(re int i=1;i<=n;i++){ while(head<=tail && a[q[tail]]<=a[i]) tail--; q[++tail]=i; while(head<=tail && q[head]+k<=i) head++; if(i>=k) cout<<a[q[head]]<<" "; } cout<<endl; } int main() { ios::sync_with_stdio(0); clock_t c1 = clock(); #ifdef LOCAL freopen("data.in","r",stdin); freopen("data.out","w",stdout); #endif // ====================================================================== cin>>n>>k; for(re int i=1;i<=n;i++) cin>>a[i]; monotoneMin(); monotoneMax(); // ====================================================================== end: cerr << "Time Used:" << clock() - c1 << "ms" << endl; return 0; }