思路:跟动态中位数一样的思路。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef pair<int,int>PII;
const int N=3e4+30;
int n,m;
int a[N],b[N];
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=m;i++)
cin>>b[i];
priority_queue<int,vector<int>,greater<int>>hm;
priority_queue<int,vector<int>,less<int>>sm;
int cnt=1;
for(int i=1,j=1;i<=n;i++)
{
if(i!=b[j])
{
hm.push(a[i]);
}else
{
hm.push(a[i]);
while(sm.size()<cnt)
{
if(hm.empty())
break;
int t=hm.top();
hm.pop();
sm.push(t);
// cout<<i<<' '<<"cao"<<endl;
}
while(sm.size()>cnt)
{
if(sm.empty())
break;
int t=sm.top();
sm.pop();
hm.push(t);
}
while(!sm.empty()&&!hm.empty()&&sm.top()>hm.top())
{
int a1=sm.top();
int b1=hm.top();
sm.pop();
hm.pop();
hm.push(a1);
sm.push(b1);
}
// cout<<i<<' '<<hm.size()<<' '<<sm.size()<<endl;
while(b[j]==i)
{
j++;
cnt++;
cout<<sm.top()<<endl;
if(hm.size()&&b[j]==i)
{
sm.push(hm.top());
hm.pop();
}
}
}
}
return 0;
}