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. Android对px和dip进行尺寸转换的方法

    px  转换为 dip /** * PX 转换为 DP * * @param context * @param px * @return */ public static int px2dp(Cont ...

  2. 能量项链AC了

    我打算写出一个尽量看起来像是人话的解题报告. 然而这道题我还是[虽然AC但不会做] OYZ

  3. 清理SharePoint 2010的SQL Server 2008 R2日志数据库的方法!

    //来源:http://www.cnblogs.com/nbpowerboy/p/3380079.html 公司用SharePoint 2010已有三年多的时间了,上BPM项目也有2年多的时间,之前供 ...

  4. Angular JS Scope(作用域)

    Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. Scope 可应用在视图和控制器上. 当你在 Ang ...

  5. div hover 特效 css样式

    -webkit-transform: scale(1.05); -moz-transform: scale(1.05); -o-transform: scale(1.05); -moz-box-sha ...

  6. 对web.config加密,和解密码详细说明

    可以使用受保护配置来加密 Web 应用程序配置文件(如 Web.config 文件)中的敏感信息(包括用户名和密码.数据库连接字符串和加密密钥).对配置信息进行加密后,即使攻击者获取了对配置文件的访问 ...

  7. Python---day5-各类模块的使用

    #_*_coding:utf-8_*_ 时间模块import time # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time() ...

  8. JAVA类与对象(课堂总结)

    一:"=="的不同含义 当"=="施加于原始数据类型变量时,是比较变量所保存的数据是否相等当"=="施加于引用类型变量时,是比较这两个变量是 ...

  9. Linux修改SSH端口和禁止Root远程登陆

    Linux修改ssh端口22 vi /etc/ssh/ssh_config vi /etc/ssh/sshd_config 然后修改为port 8888 以root身份service sshd res ...

  10. 郑州尚学堂:如何看待ARM的各种模式?

    嵌入式设备已经越来越与我们的日常生活密切相关了,由此带来了ARM的高速发展.就拿我们的手机来说吧,几乎所有的手机都是ARM体系的.这里大致介绍下ARM 的7种执行模式. ARMv4以上版本的CPU任何 ...