2251 质量检测
给定一个长度为n的数列a,求前m件的最小的值,最后全部统计
很显然,于是一个ST加上单调队列的问题,还是用两种方法来做
感觉好水
这道题和刚刚做过的一道很类似,只不过那一道很卡数据,这一个不需要
ST
#include <bits/stdc++.h>
using namespace std;
const int SIZE=1e6+5;
int st[SIZE][25];
int n,m;
inline void build()
{
for(int j=1;j<=20;j++)
{
for(int i=1;i<=m;i++)
{
if(i+(1<<j)-1<=m)
st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
}
int main()
{
cin>>m>>n;
for(int i=1;i<=m;i++)
{
cin>>st[i][0];
}
build();
for(int op=1;op<=m-n+1;op++)
{
int k=log2(n);
cout<<min(st[op][k],st[op+n-1-(1<<k)+1][k])<<endl;
}
return 0;
}
单调队列
啊啊啊啊
单调队列,是个好东西,我感觉ST完全不是人做的,有点麻烦
一般单调队列都用数组进行模拟,因为deque很慢,虽然很方便
还是挺简(mo)单(ban)的
#include <bits/stdc++.h>
using namespace std;
const int SIZE=1e6+5;
int n,m;
int a[SIZE];
int q[SIZE];
int min_queue()
{
int head=1,tail=0;
for(int i=1;i<=n;i++)
{
while(q[head]+m<=i&&head<=tail) head++;
while(a[i]<a[q[tail]]&&head<=tail) tail--;
q[++tail]=i;
if(i>=m) cout<<a[q[head]]<<endl;
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
min_queue();
return 0;
}
博客围绕2251质量检测问题展开,给定长度为n的数列a,求前m件最小值并统计。指出这是一个结合ST和单调队列的问题,可用两种方法求解。还提到单调队列通常用数组模拟,因deque虽方便但速度慢。
140

被折叠的 条评论
为什么被折叠?



