Black Box
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8637   Accepted: 3542

Description

Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are two types of transactions:

ADD (x): put element x into Black Box; 
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending. 
Let us examine a possible sequence of 11 transactions: 

Example 1

N Transaction i Black Box contents after transaction Answer

(elements are arranged by non-descending)

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

It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type. 
Let us describe the sequence of transactions by two integer arrays:

1. A(1), A(2), ..., A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2).

2. u(1), u(2), ..., u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, ... and N-transaction GET. For the Example we have u=(1, 2, 6, 6).

The Black Box algorithm supposes that natural number sequence u(1), u(2), ..., u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), ..., A(u(p)) sequence.

Input

Input contains (in given order): M, N, A(1), A(2), ..., A(M), u(1), u(2), ..., u(N). All numbers are divided by spaces and (or) carriage return characters.

Output

Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.

Sample Input

7 4
3 1 -4 2 8 -1000 2
1 2 6 6

Sample Output

3
3
1
2

题目大意:

   有多组测试数据,第一行输入N,Q,表示有N个数据要输入,有Q个询问。

  第二行输入N个数,表示有一个(存在N个数据)的数据集。

  第三行输入Q个数,设每个数位qi,第i个数位qi,表示输入这些数据集的前qi个数,求在这些数据集第i小的数据为多少;

解法:

   由于每次询问的第K小的数是不固定的,K是递增的,可以采用两个堆(小顶堆和大顶堆)来实现数据的维护、

  1.我们要保证的是让小顶堆的每一个元素都比大顶堆的中的每一个元素大。

  2.保证大顶堆中有K个元素(从小顶堆取堆顶元素,直到大顶堆有K个元素),大顶堆的堆顶的元素既为所以数据的第N小的数据。

  3。维护的话,只需要判断这小顶堆的堆顶元素是否大于大顶堆的堆顶元素,如果大于的话,就不用再交换,如果小于的话,需要交换两个堆顶元素然后再重复判断,直到,小顶堆的堆得堆顶元素都大于大顶堆的堆顶元素。

 #include <iostream>
#include <queue>
#include <stdio.h>
#define MAX 30010
using namespace std;
struct Node1
{
int S;
friend bool operator <(Node1 a,Node1 b)
{return a.S>b.S;}
};
struct Node2
{
int S;
friend bool operator <(Node2 a,Node2 b)
{return a.S<b.S;}
};
int main()
{
int N,M,i,j,k,Q,A,B,Sign,t=;
Node1 Num1;
Node2 Num2;
int NUM[MAX];
priority_queue<Node1>ID1;/*小顶堆*/
priority_queue<Node2>ID2;/*大顶堆*/
while(scanf("%d%d",&N,&M)!=EOF)
{
for(i=;i<N;i++)
{
scanf("%d",&NUM[i]);
}
Sign=;j=;
for(i=;i<M;i++)
{
scanf("%d",&Q);
while(j<Q&&j<N)
{ /*添加小顶堆元素*/
Num1.S=NUM[j++];
ID1.push(Num1);
}
for(k=ID2.size();k<Sign;k++)
{ /*求第K小的数,保证大顶堆有K个数*/
Num2.S=ID1.top().S;ID1.pop();
ID2.push(Num2);
} while(ID2.size()>&&ID1.top().S<ID2.top().S)
{ /*维护*/
Num1.S=ID2.top().S;ID2.pop();
ID1.push(Num1); Num2.S=ID1.top().S;ID1.pop();
ID2.push(Num2);
}
printf("%d\n",ID2.top().S);
Sign++;
}
}
return ;
}

【优先队列-求第Ki大的数】Black Box的更多相关文章

  1. 《数据结构与算法分析:C语言描述》读书笔记------练习1.1 求第K大的数

    求一组N个数中的第k个最大者,设k=N/2. import java.util.Random; public class K_Max { /** * @param args */ //求第K大的数,保 ...

  2. 无序数组求第K大的数

    问题描述 无序数组求第K大的数,其中K从1开始算. 例如:[0,3,1,8,5,2]这个数组,第2大的数是5 OJ可参考:LeetCode_0215_KthLargestElementInAnArra ...

  3. W - Doom HDU - 5239 线段树 找取模的规律+求一个很大的数的平方对一个数取模的写法 特别的模数==2^63-2^31

    这个题目一开始感觉还是有点难的,这个模数这么大,根本就不知道怎么写,然后去搜了题解,知道了怎么去求当x很大的时候x的平方对一个数取模怎么样不会爆掉. 然后还顺便发现了一个规律就是当一个数更新一定次数之 ...

  4. sql求倒数第二大的数,效率不高,但写法新颖

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. 求第k大的数(用到快速排序算法的思想)

    //下面两种part效率比较:相同运算量下part比part2快5倍左右,part2写法简单但是效率低 #include "stdafx.h" #include <iostr ...

  6. POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)

    传送门 The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8690   Acce ...

  7. 寻找数列中第k大的数算法分析

    问题描述:给定一系列数{a1,a2,...,an},这些数无序的,现在求第k大的数. 看到这个问题,首先想到的是先排序,然后直接输出第k大的数,于是得到啦基于排序的算法 算法一: #include&l ...

  8. [LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)

    传送门 Description There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the m ...

  9. 求第k小的数

    题目链接:第k个数 题意:求n个数中第k小的数 题解: //由快速排序算法演变而来的快速选择算法 #include<iostream> using namespace std; const ...

随机推荐

  1. C#隐藏tabcontrol

    //tabControl1.SizeMode = TabSizeMode.Fixed; //tabControl1.ItemSize = new Size(0, 1);

  2. shell-改变分隔符

    转化为换行符: oldIFS=${IFS}; IFS=$'\n'; 命令; IFS=${oldIFS};

  3. Python学习笔记——基础篇【第二周】——解释器、字符串、列表、字典、主文件判断、对象

    目录 1.Python介绍 2.Python编码 3.接受执行传参 4.基本数据类型常用方法 5.Python主文件判断 6.一切事物都是对象 7.   int内部功能介绍 8.float和long内 ...

  4. [转] Gvim for windows中块选择的方法

    在gvim(windows版)中,块选择的快捷键不是<Ctrl-v>,此快捷键为粘贴. 一般的选择模式为:v(小写),此时会显示:可视. 行选择模式为:V(大写),此时会显示:可视-行. ...

  5. Objective-C Runtime 运行时之一:类与对象(转载)

    Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理.这种动态语言的优势在于:我们写代码时更具灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一 ...

  6. something funny

    something funny. #include <stdio.h> #include <windows.h> #define N 50 HANDLE hConsole; v ...

  7. c++字体对齐

    字节对齐的细节和编译器实现相关,但一般而言,满足三个准则:    1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除.   2) 结构体每个成员相对于结构体首地址的偏移量(offset)都是 ...

  8. mysql修改编码

    1.查看当前编码 2.设置utf8mb4编码(也可以是其他),修改my.cnf或my.ini

  9. Android:Asmack能登录但是获取不到联系人的问题

    先说一下:换了N个jar包,换了N个OpenFire,就是获取不到联系人.但是能登录.特别纳闷,百度不到,google一下,有人隐约说了下权限的问题. 终于搞出来了,总结一下,浪费了一整天的时间.(码 ...

  10. redis安装以及远程连接

    第一步下载: Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases. 第二步: 运行安装 记录安装路径 C:\Program Fil ...