跪了,先存在这以后再解决!我用标准程序试了好几组数据,都和我的是对得上的,而且我还特地把M啊INF啊什么的往大地开,没超出范围,就怕不够范围,可是一直WA,哭了,难道我没理解堆的含义吗/
#include<iostream>
#include<string>
using namespace std;
#define M 100000
#define INF 201000000
struct heap
{
int size;
int date[M];
};
heap sheap,bheap; //小顶堆和大顶堆
int a[M],b[M];
void insertb(int x) //大顶堆的插入
{
int i,t;
bheap.date[++bheap.size]=x;
i=bheap.size;
while(i/2!=0&&bheap.date[i/2]<bheap.date[i])
{
t=bheap.date[i/2];
bheap.date[i/2]=bheap.date[i];
bheap.date[i]=t;
i=i/2;
}
/*printf("%d to insertb!bigtop=%d\n",x,bheap.date[1]);*/
}
void inserts(int x) //小顶堆的插入
{
int i,t;
sheap.date[++sheap.size]=x;
i=sheap.size;
while(i/2!=0&&sheap.date[i/2]>sheap.date[i])
{
t=sheap.date[i/2];
sheap.date[i/2]=sheap.date[i];
sheap.date[i]=t;
i=i/2;
}
/*printf("%d to inserts!smalltop=%d\n",x,sheap.date[1]);*/
}
void delb() //大顶堆的删除顶点
{
int k=1;
bheap.size--;
bheap.date[k]=-INF;
while(bheap.date[k*2]!=-INF||bheap.date[k*2+1]!=-INF)
{
if(bheap.date[k*2]>bheap.date[k*2+1])
{
bheap.date[k]=bheap.date[k*2];
k=k*2;
bheap.date[k]=-INF;
}
else
{
bheap.date[k]=bheap.date[k*2+1];
k=k*2+1;
bheap.date[k]=-INF;
}
}
/*printf("delb!bigtop=%d\n",bheap.date[1]);*/
}
void dels() //小顶堆的删除顶点
{
int k=1;
sheap.size--;
sheap.date[k]=INF;
while(sheap.date[k*2]!=INF||sheap.date[k*2+1]!=INF)
{
if(sheap.date[k*2]<sheap.date[k*2+1])
{
sheap.date[k]=sheap.date[k*2];
k=k*2;
sheap.date[k]=INF;
}
else
{
sheap.date[k]=sheap.date[k*2+1];
k=k*2+1;
sheap.date[k]=INF;
}
}
/*printf("dels!smalltop=%d\n",sheap.date[1]);*/
}
int main()
{
int i,t,m,n,j,x,y;
cin>>m>>n;
sheap.size=bheap.size=0;
for(i=0;i<=M;i++)
{sheap.date[i]=INF;bheap.date[i]=-INF;}
for(i=0;i<m;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
for(i=1,j=0;i<=n;i++)
{
for(;j<b[i];j++)
{
inserts(a[j]); //插入小顶堆中
if(bheap.date[1]>sheap.date[1]) //如果大顶堆中的顶点比小顶堆顶点大,则交换
{
x=bheap.date[1];
y=sheap.date[1];
delb();insertb(y);
dels();inserts(x);
}
}
while(sheap.size>1&&bheap.size<i-1) //如果大顶堆中的数量少于i-1,就将小顶堆中拉点进去
{
insertb(sheap.date[1]);
dels();
}
cout<<sheap.date[1]<<endl;
}
return 0;
}