poj - 1442 Black Box

好久没刷题了,脑子都锈掉了。这题是用优先队列做的,而且是STL里的。STL里的priority_queue默认是大堆,你可以理解为递减数列。如果要用小堆需要一点参数,我只记得一个简单些的方法:priority_queue<T, vector<T>, greater<T> >,T表示数据类型,这样就是一个小堆了。做这题时我“参考”了别人的代码,不过原版的注释写得很混乱,不知道是不是用来忽悠人的。以下是代码:

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 int A[30010],u[30010];
 5 int main()
 6 {
 7     int m,n,i,j,t;
 8     while(cin>>m>>n)
 9     {
10         priority_queue <int, vector<int> > small;
11         priority_queue <int, vector<int>,greater<int> > big;
12         for(i = 0; i < m; i++)
13             cin>>A[i];
14         for(j = 0; j < n; j++)
15             cin>>u[j];
16         j = 0;
17         for(i = 0; i < m && j < n; i++)
18         {
19             if(small.empty())
20                 big.push(A[i]);
21             else if(A[i] < small.top())
22             {
23                 t = small.top();
24                 small.pop();
25                 big.push(t);
26                 small.push(A[i]);
27             }
28             else big.push(A[i]);
29             while(u[j] == i+1)
30             {
31                 j++;
32                 t = big.top();
33                 big.pop();
34                 cout<<t<<endl;
35                 small.push(t);
36             }
37         }
38     }
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/lzxskjo/archive/2012/07/10/2585449.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值