标签:
才发现我的第一道BZ是单调队列。。
发现云神的资料里面有这个的介绍就来水水。。
单调队列由于要去掉队尾就还是用数组好点,而且写起来比STL要好看点
1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 #include<queue> 7 #define inc(i,l,r) for(int i=l;i<=r;i++) 8 #define dec(i,l,r) for(int i=l;i>=r;i--) 9 #define link(x) for(edge *j=h[x];j;j=j->next) 10 #define mem(a) memset(a,0,sizeof(a)) 11 #define inf 1e9 12 #define ll long long 13 #define succ(x) (1<<x) 14 #define NM 1000000+5 15 using namespace std; 16 int read(){ 17 int x=0,f=1;char ch=getchar(); 18 while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();} 19 while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar(); 20 return x*f; 21 } 22 int n,m,q[NM],a[NM],qh,qt,k; 23 int main(){ 24 // freopen("data.in","r",stdin); 25 n=read();k=read(); 26 inc(i,1,n)a[i]=read();qh=1; 27 inc(i,1,k-1){ 28 while(qh<=qt&&a[q[qt]]>=a[i])qt--; 29 q[++qt]=i; 30 } 31 inc(i,k,n){ 32 while(qh<=qt&&a[q[qt]]>=a[i])qt--; 33 while(qh<=qt&&q[qh]<i-k+1)qh++; 34 q[++qt]=i;printf("%d ",a[q[qh]]); 35 } 36 printf("\n");mem(q);qh=1; 37 inc(i,1,k-1){ 38 while(qh<=qt&&a[q[qt]]<=a[i])qt--; 39 q[++qt]=i; 40 } 41 inc(i,k,n){ 42 while(qh<=qt&&a[q[qt]]<=a[i])qt--; 43 while(qh<=qt&&q[qh]<i-k+1)qh++; 44 q[++qt]=i;printf("%d ",a[q[qh]]); 45 } 46 printf("\n"); 47 return 0; 48 }
标签:
原文地址:http://www.cnblogs.com/onlyRP/p/5131413.html