POJ1442(Black Box)
优先队列的题。进行队列弹出操作时要注意判空,如果有多case,记得清空队列。
View Code
1 #include <stdio.h> 2 #include <queue> 3 #define N 30000 4 using namespace std; 5 6 priority_queue<int,vector<int>,greater<int> >qmin; 7 priority_queue<int,vector<int>,less<int> >qmax; 8 9 int a[N]; 10 11 int main() 12 { 13 int i,k,n,m,x,t1,t2; 14 while(~scanf("%d%d",&n,&m)) 15 { 16 for(i=0;i<n;i++) scanf("%d",&a[i]); 17 for(i=k=0;k<m;k++) 18 { 19 scanf("%d",&x); 20 while(qmin.size()+qmax.size()<x) qmin.push(a[i++]); 21 qmax.push(qmin.top()),qmin.pop(); 22 while(!qmin.empty()&&qmax.top()>qmin.top()) 23 { 24 t1=qmin.top(),qmin.pop(); 25 t2=qmax.top(),qmax.pop(); 26 qmin.push(t2); 27 qmax.push(t1); 28 } 29 printf("%d\n",qmax.top()); 30 } 31 while(!qmin.empty()) qmin.pop(); 32 while(!qmax.empty()) qmax.pop(); 33 } 34 return 0; 35 }