设为首页 加入收藏

TOP

uva 501 - Black Box(优先队列)
2015-07-20 17:50:27 来源: 作者: 【 】 浏览:3
Tags:uva 501 Black Box 优先 队列

题目链接:uva 501 - Black Box

题目大意:有一个集合,给定元素进入集合的顺序,现在有Q次查询,给定每次查询在第几个元素进入集合后,对于每i次查询,输出集合中第i小的数。

解题思路:用两个优先队列维护,队列a优先出值大的,队列b优先出值小的,在第i次询问前,保证a队列中有i-1个元素元素,并且抱枕都比b中的小,然后每次询问输出b队列的首元素,并且将它放到a队列中。

#include 
   
     #include 
    
      #include 
     
       #include 
      
        using namespace std; const int maxn = 30005; int N, M, arr[maxn], v[maxn]; void solve () { priority_queue
       
         a; priority_queue
        
         , greater
         
           > b; for (int i = 1; i <= N; i++) { a.push(arr[i]); b.push(a.top()); a.pop(); while (v[i]--) { printf("%d\n", b.top()); a.push(b.top()); b.pop(); } } } int main () { int cas; scanf("%d", &cas); while (cas--) { int x; memset(v, 0, sizeof(v)); scanf("%d%d", &N, &M); for (int i = 1; i <= N; i++) scanf("%d", &arr[i]); for (int i = 0; i < M; i++) { scanf("%d", &x); v[x]++; } solve(); if (cas) printf("\n"); } return 0; }
         
        
       
      
     
    
   
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 3680 Intervals(费用流+离散.. 下一篇poj1159 Palindrome(最长公共子序..

评论

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

·switch520最新的地址 (2025-12-24 19:19:41)
·微信聊天功能使用了 (2025-12-24 19:19:39)
·websocket和普通的so (2025-12-24 19:19:36)
·Python中文网 - 人生 (2025-12-24 18:49:47)
·【整整648集】这绝对 (2025-12-24 18:49:44)