oval_m @ 2022-02-17 15:22:44
不吸氧 #2TLE
#include<iostream>
#include<queue>
using namespace std;
int arr[1000005],n,k;
struct cmp1{bool operator () (const int &a,const int &b){return arr[a]>arr[b];}};
struct cmp2{bool operator () (const int &a,const int &b){return arr[a]<arr[b];}};
priority_queue<int,vector<int>,cmp1> qmin;
priority_queue<int,vector<int>,cmp2> qmax;
int ansmin[1000005],ansmax[1000005];
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;i++)cin>>arr[i];
for(int i=0;i<k-1;i++){qmin.push(i);qmax.push(i);}
for(int i=0;i<n-k+1;i++)
{
qmin.push(i+k-1);
qmax.push(i+k-1);
while(qmin.top()<i)qmin.pop();
while(qmax.top()<i)qmax.pop();
ansmin[i]=arr[qmin.top()];
ansmax[i]=arr[qmax.top()];
}
for(int i=0;i<n-k+1;i++)cout<<ansmin[i]<<' ';
cout<<endl;
for(int i=0;i<n-k+1;i++)cout<<ansmax[i]<<' ';
}
by oval_m @ 2022-02-17 15:28:54
@Dream_weavers ???
by dhclient_eth1 @ 2022-02-17 15:34:34
@oval_m 为啥不吸氧
by oval_m @ 2022-02-17 15:45:46
@dhclient_eth1 可以优化到不吸氧能过把
by 飞风追云 @ 2022-02-17 15:50:40
@oval_m 单调队列这个算法本身就不用优先队列,写这么多单调队列干什么,优先队列常数并不小,容易出现被卡常的问题
单调队列是用数组模拟双端队列,求连续子段中具有一定特性的值的算法
by oval_m @ 2022-02-17 16:09:58
@飞风追云 懂了,感谢
by strcmp @ 2022-02-17 16:13:00