Ender_Fish @ 2022-08-10 21:03:50
我用的是大根堆和小根堆,不开O2是1.20秒。请问怎么优化?
#include<bits/stdc++.h>
using namespace std;
struct node
{
int val,i;
bool operator < (const node& n1) const{
return val<n1.val;
}
bool operator > (const node& n1) const{
return val>n1.val;
}
}a[1000005];
priority_queue<node> q;
priority_queue<node,vector<node>,greater<node> > q1;
int n,k;
int main()
{
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].i=i;
}
for(int i=1;i<=k;i++)
{
q1.push(a[i]);
}
for(int i=k;i<=n;i++)
{
while(q1.top().i<i-k+1)
{
q1.pop();
}
printf("%d ",q1.top().val);
//cout<<q1.top().val<<" ";//最小值
if(i+1<=n)
{
q1.push(a[i+1]);
}
}
cout<<'\n';
for(int i=1;i<=k;i++)
{
q.push(a[i]);
}
for(int i=k;i<=n;i++)
{
while(q.top().i<i-k+1)
{
q.pop();
}
printf("%d ",q.top().val);
//cout<<q.top().val<<" ";//最大值
if(i+1<=n)
{
q.push(a[i+1]);
}
}
return 0;
}
by Ender_Fish @ 2022-08-11 17:20:27
@sb_yyds 啊是吗?我今年开始才注册洛谷的,不很清楚这些,见谅
by _XHY20180718_ @ 2022-10-07 19:07:28
@Ender_Wish 现在 CSP-J/S 都有O2...