Sweetness @ 2019-08-04 21:37:43
救救孩子吧!TLE#2#3,开O2也只能过#2.
#include<bits/stdc++.h>
#define mp make_pair
#define pl pair<int,int>
using namespace std;
inline int read(){
int ans=0;
char last=' ',ch=getchar();
while(ch>'9'||ch<'0') last=ch,ch=getchar();
while(ch>='0'&&ch<='9') ans=(ans<<1)+(ans<<3)+ch-'0',ch=getchar();
if(last=='-') ans=-ans;
return ans;
}
int n,k;
int a;
int xiao[1000010],da[1000010];
priority_queue <pl,vector<pl>,greater<pl> > q;//xiao
priority_queue <pl,vector<pl>,less<pl> > Q;//da
int main(){
n=read();
k=read();
for(int i=1;i<=k;i++) {
a=read();
q.push(mp(a,i));
Q.push(mp(a,i));
}
int cnt=0;
xiao[++cnt]=q.top().first;
da[cnt]=Q.top().first;
for(int i=k+1;i<=n;i++){
while(q.top().second<=i-k) q.pop();
while(Q.top().second<=i-k) Q.pop();
a=read();
q.push(mp(a,i));
Q.push(mp(a,i));
xiao[++cnt]=q.top().first;
da[cnt]=Q.top().first;
}
for(int i=1;i<=cnt;i++) printf("%d ",xiao[i]);
puts("");
for(int i=1;i<=cnt;i++) printf("%d ",da[i]);
return 0;
}
by LordLeft @ 2019-08-04 21:44:35
改成deque的写法吧,难道不是那种才叫单调队列吗
by We_Lost_The_Sea @ 2019-08-04 21:50:28
楼主,你这个是优先队列啊,时间复杂度是O(nlogn)的,很容易T,而单调队列是O(n)的。