【优先队列-求第Ki大的数】Black Box
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 8637 | Accepted: 3542 |
Description
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:
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).
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
Output
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的更多相关文章
- 《数据结构与算法分析:C语言描述》读书笔记------练习1.1 求第K大的数
求一组N个数中的第k个最大者,设k=N/2. import java.util.Random; public class K_Max { /** * @param args */ //求第K大的数,保 ...
- 无序数组求第K大的数
问题描述 无序数组求第K大的数,其中K从1开始算. 例如:[0,3,1,8,5,2]这个数组,第2大的数是5 OJ可参考:LeetCode_0215_KthLargestElementInAnArra ...
- W - Doom HDU - 5239 线段树 找取模的规律+求一个很大的数的平方对一个数取模的写法 特别的模数==2^63-2^31
这个题目一开始感觉还是有点难的,这个模数这么大,根本就不知道怎么写,然后去搜了题解,知道了怎么去求当x很大的时候x的平方对一个数取模怎么样不会爆掉. 然后还顺便发现了一个规律就是当一个数更新一定次数之 ...
- sql求倒数第二大的数,效率不高,但写法新颖
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 求第k大的数(用到快速排序算法的思想)
//下面两种part效率比较:相同运算量下part比part2快5倍左右,part2写法简单但是效率低 #include "stdafx.h" #include <iostr ...
- POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)
传送门 The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8690 Acce ...
- 寻找数列中第k大的数算法分析
问题描述:给定一系列数{a1,a2,...,an},这些数无序的,现在求第k大的数. 看到这个问题,首先想到的是先排序,然后直接输出第k大的数,于是得到啦基于排序的算法 算法一: #include&l ...
- [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 ...
- 求第k小的数
题目链接:第k个数 题意:求n个数中第k小的数 题解: //由快速排序算法演变而来的快速选择算法 #include<iostream> using namespace std; const ...
随机推荐
- Mysq 5.7l服务无法启动,没有报告任何错误
昨天系统崩溃了,然后重装了Mysql 5.7 安装步骤和遇到问题及解决方案. 去官网下载Mysql 5.7的解压包(zip),解压到你要安装的目录. 我的安装目录是:D:\Java\Mysql 安装步 ...
- [SQL基础教程] 3-3 HAVING
[SQL基础教程] 3-3 HAVING HAVING子句 SELECT col_1,col_2 FROM table GROUP BY col_1,col_2 HAVING col_1 = '2'; ...
- iOS跳转系统设置界面
iOS开发之如何跳到系统设置里的各种设置界面:http://www.superqq.com/blog/2015/12/01/jump-setting-per-page/ iOS:你App的设置做对了吗 ...
- ubuntu 安装Matlab 解决显示中文乱码
在ubuntu 14.04中安装Matlab 2015a打开后发现中文乱码,这主要是由于JAVA中文支持问题. 解决方法如下: 进入Maltab的安装路径: 进入JRE目录: cd Matlab目 ...
- 在一个form里边同时执行搜索和 execl导出功能
一个form 分搜索 和 导出<form name="searchform" id="searchform" > <input type=&q ...
- CentOS6.5 安装mysql5.6.30
1.下载解压由于系统会自带mysql5.1版本的数据库,需要卸载.[root@localhost src]# yum remove -y mysql-libs[root@localhost src]# ...
- Webdriver其他定位方式
1.下拉框的定位 在遇到select下拉框的选择时,比如: <select id="nr" name="NR"> <option select ...
- .net 获取类型的Type类型的几种方法
一:使用Object基类的GetType()方法 Car car = new Car(); Type carType = car.GetType(); 二:使用typeof操作符 Type carTy ...
- 异步设备IO:OVERLAPPED和IOCompletionPort
异步设备IO:OVERLAPPED和IOCompletionPort 本文内容为<windows核心编程>第10章内容的总结,仅记录一些本人感兴趣的内容. 1:OVERLAPPED &qu ...
- css3技巧属性之text-overflow
text-overflow:clip | ellipsis 默认值:clip 取值: clip: 当对象内文本溢出时不显示省略标记(...),而是将溢出的部分裁切掉. ellipsis: 当对象内文本 ...