题目描写叙述 Description

我们使用黑匣子的一个简单模型。它能存放一个整数序列和一个特别的变量i。在初始时刻。黑匣子为空且i等于0。

这个黑匣子能运行一系列的命令。有两类命令:

ADD(x):把元素x放入黑匣子。GET:把i加1的同一时候,输出黑匣子内全部整数中第i小的数。牢记第i小的数是当黑匣子中的元素已非降序排序后位于第i位的元素。

以下的表6_4是一个11个命令的样例:

表6_4

编号

命令

i

黑匣子内容

输出

1

ADD(3)

0

3

2

GET

1

3

3

3

ADD(1)

1

1,3

4

GET

2

1,3

3

5

ADD(-4)

2

-4,1,3

6

ADD(2)

2

-4,1,2,3

7

ADD(8)

2

-4,1,2,3,8

8

ADD(-1000)

2

-1000,-4,1,2,3,8

9

GET

3

-1000,-4,1,2,3,8

1

10

GET

4

-1000,-4,1,2,3,8

2

11

ADD(2)

4

-1000,-4,1,2,2,3,8

现须要一个有效的算法处理给定的一系列命令。

ADD和GET命令的总数至多个有30000个。定义ADD命令的个数为M个。GET命令的个数为N个。

我们用以下得两个整数序列描写叙述命令序列:

1.A(1),A(2),……,A(M):增加黑匣子的元素序列。全部的数均为绝对值不超过2000000的整数。

比如在上例中A=(3,1,-4,2,8,-1000,2)。

2.u(1),u(2),……,u(N):u(i)表示第i个GET命令在第u(i)个ADD命令之后,比如在上例中,u=(1,2,6,6)。

你能够假定自然数序列u(1),u(2),……,u(N)以非降序排列。N≤M,且对于每个p(1≤p≤N)有p≤u(p)≤M。

输入描写叙述 Input Description

第一行存放M和N的值,第二行存放 A(1),A(2),……,A(M) ,第三行存放u(1),u(2),……,u(N)。

输出描写叙述 Output Description

输出黑匣子的处理结果。

例子输入 Sample Input

7 4

3 1 -4 2 8 -1000 2

1 2 6 6

例子输出 Sample Output

3

3

1

2

刚開始并不知道这题该怎样下手。知道是堆做了。

可是详细也不知道怎么做。

看了这第二个解题报告了才知道怎样做:http://www.wikioi.com/solution/list/2573/(第二个解题报告,思想非常好)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<bitset>
#define INF 100007
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
priority_queue<int,vector<int>,greater<int> >heap_small;
priority_queue<int>heap_big;
int a[30005],b[30005];
int main()
{
int n,k,i,j,ii=0,jj=-1;
cin>>n>>k;
for(i=1; i<=n; i++)
scanf("%d",a+i);
for(i=0; i<k; i++)
scanf("%d",b+i);
for(i=1; i<=n; i++)
{
if(jj<ii) heap_big.push(a[i]),jj++;
else
{
int ans=heap_big.top();
if(a[i]>=ans) heap_small.push(a[i]);
else
{
heap_big.pop();
heap_small.push(ans);
heap_big.push(a[i]);
}
}
while(i==b[jj])
{
printf("%d\n",heap_big.top());
ii++;
if(jj+1<k&&i==b[jj+1])
{
int ans=heap_small.top();
heap_small.pop();
heap_big.push(ans);
jj++;
}
else break;
}
if(ii>=k) break;
}
return 0;
}

wikioi 2573 大顶堆与小顶堆并用的更多相关文章

  1. 堆排序(大顶堆、小顶堆)----C语言

    堆排序 之前的随笔写了栈(顺序栈.链式栈).队列(循环队列.链式队列).链表.二叉树,这次随笔来写堆 1.什么是堆? 堆是一种非线性结构,(本篇随笔主要分析堆的数组实现)可以把堆看作一个数组,也可以被 ...

  2. heap c++ 操作 大顶堆、小顶堆

    在C++中,虽然堆不像 vector, set 之类的有已经实现的数据结构,但是在 algorithm.h 中实现了一些相关的模板函数.下面是一些示例应用 http://www.cplusplus.c ...

  3. 《排序算法》——堆排序(大顶堆,小顶堆,Java)

    十大算法之堆排序: 堆的定义例如以下: n个元素的序列{k0,k1,...,ki,-,k(n-1)}当且仅当满足下关系时,称之为堆. " ki<=k2i,ki<=k2i+1;或k ...

  4. 大顶堆与小顶堆应用---寻找前k小数

    vector<int> getLeastNumber(vector<int>& arr,int k){ vector<int> vec(k,); if(== ...

  5. 378. Kth Smallest Element in a Sorted Matrix(大顶堆、小顶堆)

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  6. 数据结构:堆排序 (python版) 小顶堆实现从大到小排序 | 大顶堆实现从小到大排序

    #!/usr/bin/env python # -*- coding:utf-8 -*- ''' Author: Minion-Xu 小堆序实现从大到小排序,大堆序实现从小到大排序 重点的地方:小堆序 ...

  7. Python使用heapq实现小顶堆(TopK大)、大顶堆(BtmK小)

    Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) | 四号程序员 Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) 4 Replies 需1求:给出N长 ...

  8. 剑指offer:数据流中的中位数(小顶堆+大顶堆)

    1. 题目描述 /** 如何得到一个数据流中的中位数? 如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值. 如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两 ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. alter system register

    alter system register的用法 1 Static Registration via set the listener.ora2 Dynamic Instance Registrati ...

  2. Day22 JSONP、瀑布流

    一.JSONP JSONP a.Ajax $.ajax({ url:'/index/', dataType:'json', data:{}, type:'GET', success:function( ...

  3. 2016021801 - Java内存区域归纳对比

    线程私有 线程共享 程序计数器,虚拟机栈,本地方法栈 堆,方法区 内存区 异常 异常原因 程序计数器 无 虚拟机栈 StackOverflowError 线程请求的栈深度大于虚拟机栈所允许的深度 Ou ...

  4. 拦截QT关闭窗口的CloseEvent

    QDialog类下有一个虚函数 void QDialog::closeEvent (  QCloseEvent   *  e   )  [virtual protected] 通过实现closeEve ...

  5. Web-Scale-IT 到底是啥?

    Gartner 对 2015 年 10 大 IT 趋势的预测中有一个词条为:Web Scale IT.我们跟随 Matthias Ankli 来了解一下究竟什么是 Web Scale IT.本文译自 ...

  6. Android 使用HttpClient方式提交GET请求

    public void httpClientGet(View view) { final String username = usernameEditText.getText().toString() ...

  7. 14.7.1 Resizing the InnoDB System Tablespace InnoDB 系统表空间大小

    14.7.1 Resizing the InnoDB System Tablespace InnoDB 系统表空间大小 这个章节描述如何增加或者减少 InnoDB 系统表空间的大小 增加InnoDB ...

  8. 【HDOJ】1429 胜利大逃亡(续)

    BFS+状态压缩,做了很多状态压缩了.今晚把八数码问题给搞定了. #include <iostream> #include <queue> #include <cstri ...

  9. [LeetCode#244] Shortest Word Distance II

    Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...

  10. [LeetCode#260]Single Number III

    Problem: Given an array of numbers nums, in which exactly two elements appear only once and all the ...