设为首页 加入收藏

TOP

POJ 1442 Black Box(优先队列)
2015-07-20 17:59:40 来源: 作者: 【 】 浏览:2
Tags:POJ 1442 Black Box 优先 队列

题目地址:POJ 1442

这题是用了两个优先队列,其中一个是较大优先,另一个是较小优先。让较大优先的队列保持k个。每次输出较大优先队列的队头。

每次取出一个数之后,都要先进行判断,如果这个数比较大优先的队列的队头要小,就让它加入这个队列,队列头移到较小优先的队列中。然后当较大优先的数不足k个的时候,就让较小优先的队列的队头移到较大优先的队头中。

代码如下;

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
           #include 
           
             #include 
            
              using namespace std; int a[40000], b[40000]; struct xiao { int x; bool operator < (const xiao &a) const { return x > a.x; } }; struct da { int x; bool operator < (const da &a) const { return x
             
              q1; priority_queue
              
               q2; xiao f1; da f2; for(i=0; i
               
                =q2.top().x) { f1.x=a[j]; q1.push(f1); } else { f1.x=q2.top().x; q1.push(f1); q2.pop(); f2.x=a[j]; q2.push(f2); } j++; } while(q2.size()<=i) { f1=q1.top(); f2.x=f1.x; q1.pop(); q2.push(f2); } printf("%d\n",q2.top()); } } return 0; } 
               
              
             
            
           
         
        
       
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇STL--H - Black Box(两个优先队.. 下一篇poj 3400 Dropping the stones

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: