优先队列+哈希记录窗口内元素个数TLE#4
查看原帖
优先队列+哈希记录窗口内元素个数TLE#4
419173
hiro653楼主2022/11/17 09:39

因为数的范围是int,在记录窗口内元素的个数的时候使用了map,超时了4个点,这是为什么呢,这个算法应该是nlognnlogn

//#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
//大根
priority_queue <int,vector<int>,greater<int>>q_sml;
//大根堆
priority_queue<int> q_big;
int p[1000005];
int sml[1000005];
int big[1000005];
int cnt=0;
map<int,int> mp;
int main() {

   int n,k;
   cin>>n>>k;
   for(int i=1;i<=k;i++){

    scanf("%d",&p[i]);
    mp[p[i]]++;
    q_big.push(p[i]);
    q_sml.push(p[i]);
   }
   sml[++cnt]=q_sml.top();
   big[cnt]=q_big.top();
   //v_s.push_back(q_sml.top());
   //v_b.push_back(q_big.top());
   //cout<<q_sml.top()<<' '<<q_big.top()<<endl;
   for(int i=k+1;i<=n;i++){
     scanf("%d",&p[i]);
    mp[p[i-k]]--;
    mp[p[i]]++;
    q_sml.push(p[i]);
    q_big.push(p[i]);
    while(!q_sml.empty()&&mp[q_sml.top()]==0){
        q_sml.pop();
    }
    while(!q_big.empty()&&mp[q_big.top()]==0){
        q_big.pop();
    }
   sml[++cnt]=q_sml.top();
   big[cnt]=q_big.top();
    //v_s.push_back(q_sml.top());
   //v_b.push_back(q_big.top());
    //cout<<q_sml.top()<<' '<<q_big.top()<<endl;
   }
   for(int i=1;i<=cnt;i++){
 
      printf("%d ",sml[i]);
   }
   cout<<endl;
   for(int i=1;i<=cnt;i++){
 
      printf("%d ",big[i]);
   }
   
}
2022/11/17 09:39
加载中...