企图拿优先队列水过去

P1886 滑动窗口 /【模板】单调队列

肥嘟嘟左卫门 @ 2022-07-14 14:31:03

//企图拿优先队列水过去,结果卡了一个点(90分)果然复杂度差了点,有大佬能提一些优化建议吗

#include<bits/stdc++.h>
using namespace std;
const long long maxx=0x3f3f3f3f3f3f3f3f;
const long long minn=0xc0c0c0c0c0c0c0c0;
const double pi = 4.0*atan(1.0);
#define int long long
#define f(i,n,m) for(long long i=n;i<=m;++i)
#define unf(i,n,m) for(long long i=n;i>=m;--i)
#define kong NULL
#define debug cout<<"sss"<<endl;

struct ww{
    int val;int wei;
};
struct pan{
    bool operator()(ww x,ww y){
        return x.val<y.val;//大的在前面
    } 
};
struct pan2{
    bool operator()(ww x,ww y){
        return x.val>y.val;//小的在前面
    } 
};
priority_queue<ww,vector<ww>,pan>que;
priority_queue<ww,vector<ww>,pan2>que2;
int n,k;
ww a[1000010];
int sum[1000010];
int sum2[1000010];
void solve() {
cin>>n>>k;
f(i,1,n){
    cin>>a[i].val;
    a[i].wei=i;
}
f(i,1,k){
    que.push(a[i]);
    que2.push(a[i]);
}
sum[k]=que.top().val;
sum2[k]=que2.top().val;
f(i,k+1,n){
    while((!que.empty())&&(i-k>=que.top().wei)){
        que.pop();
    }
    while((!que2.empty())&&(i-k>=que2.top().wei)){
        que2.pop();
    }
    que.push(a[i]);
    que2.push(a[i]);
    sum[i]=que.top().val;
    sum2[i]=que2.top().val;
}
f(i,k,n)cout<<sum2[i]<<" ";
cout<<endl;
f(i,k,n)cout<<sum[i]<<" ";

}

signed main( )
{
ios::sync_with_stdio(false);
solve();
return 0;
}

by Coros_Trusds @ 2022-07-14 14:32:14

@肥嘟嘟左卫门 开 O2 就过了


by StarLbright40 @ 2022-07-14 14:32:30

建议学习单调队列


by 肥嘟嘟左卫门 @ 2022-07-14 14:33:07

@Coros_Trusds ?什么o2


by waauto @ 2022-07-14 14:33:41

O2


by 晴空一鹤 @ 2022-07-14 14:33:51

快读快熟

试试 inline


by Coros_Trusds @ 2022-07-14 14:34:07

@肥嘟嘟左卫门 提交代码窗口有一个 开启O2优化,勾选


by Azure__ @ 2022-07-14 14:34:37

学习单调队列才是正解


by ningago @ 2022-07-14 14:34:59

优先队列和单调队列有很大关系吗?


by AKNOI的梓钦 @ 2022-07-14 14:35:02

吸氧


by xfrvq @ 2022-07-14 14:42:04

你这个正确性保证吗


| 下一页