每日一题 day26 打卡
Analysis
单调队列模板
对于每一个区间,有以下操作:
1、维护队首(就是如果你已经是当前的m个之前那你就可以被删了,head++)
2、在队尾插入(每插入一个就要从队尾开始往前去除冗杂状态)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define int long long 6 #define maxn 1000000+10 7 using namespace std; 8 inline int read() 9 { 10 int x=0; 11 bool f=1; 12 char c=getchar(); 13 for(; !isdigit(c); c=getchar()) if(c=='-') f=0; 14 for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0'; 15 if(f) return x; 16 return 0-x; 17 } 18 inline void write(int x) 19 { 20 if(x<0){putchar('-');x=-x;} 21 if(x>9)write(x/10); 22 putchar(x%10+'0'); 23 } 24 int n,k; 25 int a[maxn]; 26 int deque[maxn],deque_num[maxn]; 27 inline void solve_min() 28 { 29 int head=1,tail=0; 30 for(int i=1;i<=n;i++) 31 { 32 while(head<=tail&&deque[tail]>=a[i]) tail--; 33 deque[++tail]=a[i]; 34 deque_num[tail]=i; 35 while(head<=tail&&deque_num[head]<=i-k) head++; 36 if(i>=k) 37 { 38 write(deque[head]); 39 printf(" "); 40 } 41 } 42 printf(" "); 43 } 44 inline void solve_max() 45 { 46 int head=1,tail=0; 47 for(int i=1;i<=n;i++) 48 { 49 while(head<=tail&&deque[tail]<=a[i]) tail--; 50 deque[++tail]=a[i]; 51 deque_num[tail]=i; 52 while(head<=tail&&deque_num[head]<=i-k) head++; 53 if(i>=k) 54 { 55 write(deque[head]); 56 printf(" "); 57 } 58 } 59 } 60 signed main() 61 { 62 n=read();k=read(); 63 for(int i=1;i<=n;i++) a[i]=read(); 64 solve_min(); 65 memset(deque,0,sizeof(deque)); 66 memset(deque_num,0,sizeof(deque_num)); 67 solve_max(); 68 return 0; 69 } 70 /* 71 10 3 72 -94 21 24 73 38 77 11 73 9 -88 73 74 -94 21 24 38 11 11 9 -88 75 24 73 73 77 77 77 73 73 76 */
请各位大佬斧正(反正我不认识斧正是什么意思)