POJ 1442 Black Box(优先队列)

题目链接

题意:一个队列,一直往里插入元素,n次询问,第i次询问,询问第i小的元素。

划分树不知道能不能破(可能会超时),这个题目插入次数和询问次数都很大,但是询问是有规律的。建立两个堆,一个大顶堆,一个小顶堆,然后直接上STL就可以了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <string>
 7 #include <queue>
 8 #include <vector>
 9 using namespace std;
10 int p[30001],o[30001];
11 int main()
12 {
13     int i,j,n,m,t;
14 
15     scanf("%d%d",&n,&m);
16     for(i = 1; i <= n; i ++)
17         scanf("%d",&p[i]);
18     for(i = 1; i <= m; i ++)
19         scanf("%d",&o[i]);
20     priority_queue<int> que1;
21     priority_queue<int,vector<int>,greater<int> >que2;
22     j = 1;
23     for(i = 1; i <= n; i ++)
24     {
25         que1.push(p[i]);
26         while(que1.size() > j)
27         {
28             t = que1.top();
29             que1.pop();
30             que2.push(t);
31         }
32         while(o[j] == i)
33         {
34             j ++;
35             printf("%d\n",que1.top());
36             while(que1.size() < j)
37             {
38                 if(que2.size() != 0)
39                 {
40                     t = que2.top();
41                     que2.pop();
42                     que1.push(t);
43                 }
44                 else
45                 break;
46             }
47         }
48     }
49     return 0;
50 }

 

 

转载于:https://www.cnblogs.com/naix-x/archive/2013/01/17/2864107.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值