【优先队列-求第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 ...
随机推荐
- nginx(1)
下一篇:nginx(2) 一.engin X 市场的服务器非常的多,这里简单介绍几种常用的. Apache:源代码开放,跨平台,可移植,且支持的模块非常丰富,虽然在速度性能上不如其他轻量级的web服务 ...
- ElasticSearch(8)-分布式搜索
分布式搜索的执行方式 在继续之前,我们将绕道讲一下搜索是如何在分布式环境中执行的. 它比我们之前讲的基础的增删改查(create-read-update-delete ,CRUD)请求要复杂一些. 注 ...
- Zeppelin0.6.2使用hive解释器
Zeppelin0.6.2的jdbc Interpreter 配置 1.拷贝hive的配置文件hive-site.xml到zeppelin-0.6.2-bin-all/conf下. 2.进入conf下 ...
- GNU/Linux超级本ZaReason Ultralap 440体验
老早就在网上看到有关ZaReason公司推出了业界首款Linux超级本Ultralap 430的新闻.通过网上搜索,我了解到ZaReason是一家专门制作与销售预装Linux台式机.笔记本.超级本.平 ...
- 移动HTML 5前端性能优化指南(转载)
前端工程师的菜!最近移动Html 5越来越火,想有一个体验流畅的Html 5 应用,这篇优化指南就别放过咯.腾讯的同学将关键的注意点与优化方法都总结出来,全文高能干货,非常值得深度学习 >> ...
- 关闭Pycharm拼写检查(Mac)
1.关闭拼写检查,preference-->Editor-->Inspections-->Spelling-->Typo,取消勾选 2.关闭代码风格检查,preference- ...
- java上传并下载以及解压zip文件有时会报文件被损坏错误分析以及解决
情景描述: 1.将本地数据备份成zip文件: 2.将备份的zip文件通过sftp上传到文件服务器: 3.将文件服务器上的zip文件下载到运行服务器: 4.将下载的zip文件解压到本地(文件大小超过50 ...
- angular localStorage使用方法
var YourCtrl = function($scope, localStorageService, ...) { // To add to local storage localStorageS ...
- sql 针对多个id或名称的分割和组合
开发中经常遇到把多个id拼接在一起符号隔开传入数据库,那拆分呢就是个大事 SELECT nPushID INTO #temp FROM Table1 ), nCoulmn) SELECT * FROM ...
- python-cmp()的使用
注意:python3中已经删除了cmp()该函数. cmp(x,y) 比较x与y,当x>y时,返回1: 当x==y时,返回0: 当x<y时,返回-1: >>>cmp(1, ...