P1801 黑匣子
题解
用由于本题具有线性特征(总是不减?)所以可以用两个堆来维护第i小的元素,
code
#include<bits/stdc++.h>
using namespace std;
int a[200005];
int main()
{
ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
int sizes=0,it=1;
priority_queue<int,vector<int> ,greater<int> > qsmall;
priority_queue<int> qbig;
for(int i=1;i<=m;i++)
{
int x;
cin>>x;
while(it<=x)
{
if(qbig.size()<i-1)qbig.push(a[it]);//放入决策
else qsmall.push(a[it]);
while(qsmall.size()&&qbig.size()&&qbig.top()>qsmall.top())//维护平衡
{
int d1=qbig.top(),d2=qsmall.top();
qbig.pop();
qsmall.pop();
qbig.push(d2);
qsmall.push(d1);
}
it++;
}
while((qbig.size()<i-1))//维护大小
{
qbig.push(qsmall.top());
qsmall.pop();
}
//printf("size:%d,%d\n",qsmall.size(),qbig.size());
cout<<qsmall.top()<<endl;
}
return 0;
}