单调队列
适合解决的问题:处理子序列极值问题,例如求任一序列的最大值最小值、求最大子序列和。
核心代码
使用双端队列deque
push_back();//队尾入队
pop_back();//队尾出队
push_front();//队头入队
pop_front();//队头出队
//虽然双端队列两端都能操作,因为普通队列从尾端入队,这里均采用尾端入队。
while(!q.empty() && sum[i-1]<sum[q.back()])q.pop_back();
while(!q.empty() && q.front()<=i-k-1)q.pop_front();
q.push_back(i-1);
while(!q1.empty()&&a[i]>a[q1.back()])q1.pop_back();
q1.push_back(i);
while(!q2.empty()&&a[i]<a[q2.back()])q2.pop_back();
q2.push_back(i);
一、HDU 3415
题意:对于长度为n的循环序列,求长度<=k的子序列,使得该子序列的数字和最大。
解法:将序列扩充一倍来模拟循环序列,构建前缀和时只需要到n+k-1。遍历前缀和,对前缀和维护一个单调递增序列,每次有新成员队尾入队就将比他大的都pop掉,这样保证了队头元素是最小的,再将队头范围超过k的元素都pop出去。即可得到对于结束位置为i的最大子序列和是sum[i]-sum[q.front()]。遍历一次即可得到最终解。
#include <bits/stdc++.h>
using namespace std;
int a[200005],sum[200005];
deque<int> q;
int N,n,k;
int main() {
scanf("%d",&N);
while(N--) {
sum[0]=0;
q.clear();
scanf("%d %d",&n,&k);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
a[i+n]=a[i];//模拟环状循环序列
}
for(int i=1; i<=n+k-1; i++)sum[i]=sum[i-1]+a[i];//前缀和
int head,tail;
int ans=-100000000;
for(int i=1; i<=n+k-1; i++) {
while(!q.empty() && sum[i-1]<sum[q.back()])q.pop_back();
while(!q.empty() && q.front()<=i-k-1)q.pop_front();
q.push_back(i-1);
if(sum[i]-sum[q.front()]>ans) {
ans=sum[i]-sum[q.front()];
head=q.front()+1;
if(i>n)
tail=i%n;
else
tail=i;
}
q.push_back(i-1);
}
printf("%d %d %d\n",ans,head,tail);
}
return 0;
}
二、HDU 3530
题意:问给定序列的最长子序列,满足最大值最小值的差在[m,k]之间。
解法:维护两个单调队列,一个存最大一个存最小,发现差值大于k就更新最大值和最小值中位置最前面那个更新,小于m只能通过新加入元素解决,不需要更新。
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a[100005];
deque<int> q1, q2;
int main() {
while(~scanf("%d%d%d", &n, &m, &k)) {
int ans = 0;
while(!q1.empty()) q1.pop_back();
while(!q2.empty()) q2.pop_back();
int tmp = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
while(!q1.empty()&&a[i]>a[q1.back()])q1.pop_back();
q1.push_back(i);
while(!q2.empty()&&a[i]<a[q2.back()])q2.pop_back();
q2.push_back(i);
while(a[q1.front()]-a[q2.front()]>k) {
if(q1.front()>q2.front()) {
tmp=q2.front();
q2.pop_front();
}
else {
tmp=q1.front();
q1.pop_front();
}
}
if(!q1.empty()&&!q2.empty()&&a[q1.front()]-a[q2.front()]>=m)ans=max(ans,i-tmp);
}
printf("%d\n",ans);
}
return 0;
}
三、HDU 6319
题意:这题比较绕,大意是给长为n的序列,查询每个长度为m的子序列,从第一个元素开始的最大值,和最大值变换的次数
解法:最好的解法是逆向求解,反向维护一个递减单调队列,这样每次维护结束后里面剩下的元素个数就是最大值更新次数(这个不好想到)。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int c[10000015];
deque<int> q;
int N,n,m,k,p,Q,r,mod;
ll ans1,ans2;
int main() {
scanf("%d",&N);
while(N--) {
ans1=ans2=0;
q.clear();
scanf("%d%d%d%d%d%d%d",&n,&m,&k,&p,&Q,&r,&mod);
for(int i=1; i<=k; i++)scanf("%d",&c[i]);
for (int i=k+1; i<=n; i++)
c[i]=(1LL*p*c[i-1]+1LL*Q*i+r)%mod;
for(int i=n; i>=1; i--) {
while(!q.empty()&&c[q.back()]<=c[i])q.pop_back();
while(!q.empty()&&q.front()>i+m-1) q.pop_front();
q.push_back(i);
if(i>n-m+1) continue;
ans1+=(c[q.front()]^i);
ans2+=(q.size()^i);
}
cout<<ans1<<' '<<ans2<<endl;
}
return 0;
}
四、HDU 2559(待解决)
题意:一个序列里面有些顶点,有各点权值(垃圾)和各点间距离,选取两点,此点前面的点要将垃圾运到此处,第二个点后的垃圾运到终点去。
解法:涉及到的关系比较复杂,暂时没想出来
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int c[10000015];
deque<int> q;
ll ans1,ans2;
int main() {
return 0;
}