STL--H - Black Box(两个优先队列,求第k小的值)

H - Black Box

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d
& %I64u

Submit Status

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

题意很麻烦:解释一下数据 7 4 表示给出7个数,有4个询问,下一行给出7个数,在下一行有4个询问,“1”代表从头到第一个元素中最小的值,“2”代表从头到第二个元素中第二小的值,“6”代表从头到第六个元素中第三小的值,“6”代表从头到第六个元素中第四小的值,给出的询问中 a[i] <= a[j] (i < j) ;

做法,定义两个优先队列,以大优先的p1,以小优先的p2,如果要求的是第x小的值,p1中存下(x-1)个小值,那么第x个就是p2的队首,在求第一个小的值,p1为空,求完第一个小的值后,将p2的队首放入p1,再来求第二小的值,读取给出的数(a)时,如果a大于p1的队首,那么a放入p2,否则,将a放入p1,p1的队首放入p2,保证p1的个数均比p2小,且为(x-1)个,读取完数后p2的队首就是第x小的数,输出,再把p2的队首放入p1,执行之前的操作,得到下一个要求的最小值。

用两个优先队列,分开整体的数组,得到第x小值

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define LL __int64
priority_queue <LL> p1 ;
priority_queue <LL,vector<LL>,greater<LL> > p2 ;
LL a[6000000] ;
int main()
{
    int i , j , n , m , x ;
    LL temp ;
    while(scanf("%d %d", &n, &m)!=EOF)
    {
        while( !p1.empty() )
            p1.pop();
        while( !p2.empty() )
            p2.pop() ;
        for(i = 1 ; i <= n ; i++)
            scanf("%I64d", &a[i]);
        i = 1 ;
        while(m--)
        {
            scanf("%d", &x);
            for( ; i <= x ; i++)
            {
                if( p1.empty() || p1.top() < a[i] )
                    p2.push(a[i]);
                else
                {
                    p1.push(a[i]);
                    temp = p1.top() ;
                    p1.pop() ;
                    p2.push(temp);
                }
            }
            temp = p2.top();
            p2.pop() ;
            printf("%d\n", temp);
            p1.push(temp);
        }
    }
    return 0;
}

STL--H - Black Box(两个优先队列,求第k小的值),布布扣,bubuko.com

时间: 2024-10-07 11:18:10

STL--H - Black Box(两个优先队列,求第k小的值)的相关文章

Luogu P1923 求第k小的数

Luogu P1923 求第k小的数 一看这题,静态查询区间第$k$小的数,不就是可持久化线段树(主席树)的模板题吗?!(误) 直接把主席树的板子打上来?: #include<bits/stdc++.h> #define N 200010 #define mid ((l+r)>>1) using namespace std; int n,m,l,r,k,ans,id,siz; int a[N],b[N]; struct segmenttree { int ls,rs,sum,roo

算法导论学习之线性时间求第k小元素+堆思想求前k大元素

对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思路例如以下: 1.分解:将A[p,r]分解成A[p,q-1]和A[q+1,r]两部分.使得A[p,q-1]都小于A[q],A[q+1,r]都不小于A[q]; 2.求解:假设A[q]恰好是第k小元素直接返回,假设第k小元素落在前半区间就到A[p,q-1]递归查找.否则到A[q+1,r]中递归查找. 3

树状数组求第K小值 (spoj227 Ordering the Soldiers &amp;&amp; hdu2852 KiKi&#39;s K-Number)

题目:http://www.spoj.com/problems/ORDERS/ and http://acm.hdu.edu.cn/showproblem.php?pid=2852 题意:spoj227:告诉每个位置前面有多少个数比当前位置小,求出原序列.hdu2852:设计一个容器,支持几种操作:增加/删除元素,求容器中比a大的数中第k小的数是多少. 分析:两个题思路都是求数组里面的第K小的数.开始一直在找O(N*logN)的方法,后来发现O(N*logN*logN)也是可以过的...两步:和

Quick-Select 1亿个数快速求第K小的数 分治法

Quick-Select  1亿个数快速求第K小的数  分治法 利用快速排序的思想,一开始选取中枢元,然后左右调整,接着比对中枢元p和K的大小,如果 p+1 = k (数组从0开始), 那么a[p] 就是答案,因为在p之前的,肯定都是小于a[p]的, 在p之后的,肯定大于p, 所以 a[p] 就是第 p+1 小.假如 p+1 不等于K, 那么根据大小,进行左右调整.调整过程中,理想状态下,每次都砍掉一半,数组的起始坐标要进行调整. 代码: // 快速排序法的修改 #include <iostre

HDU 5008西安网络赛B题:后缀数组求第k小子串

思路:尼玛,这题搞了一天了,比赛的时候用了n^2的方法绝对T了,然后今天看别人代码看了一天才知道.后面感觉也挺容易的,就是没想到,之前做过SPOJ 694 705求过不同子串了,知道怎么求不同子串个数了,但是比赛的时候这个技巧竟然抛在脑后了,然后就不会了. 但是今天自己用了自己的两个后缀数组的模板(倍增和DC3)的都WA了,搞得自己真想跳楼去了!! 到现在都不知道到底是哪里错了,处理的方法和标准做法都一样,但是就是WA,然后用了别人的模板,再用自己的处理方法就过了,怀疑自己的两个模板是不是哪里错

HDU 5008 求第k小子串

本题要求第k小的distinct子串,可以根据height数组,二分出这个第k小子串所在后缀的位置信息.由于题目要求子串起始下标尽可能小.所以再在rank数组中,二分出与当前后缀LCP大于等于所求子串长度的范围.通过RMQ求出这个范围中最小的sa. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <string> 5 #include <string

hdu3949(线性基,求第k小的异或和

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4731    Accepted Submission(s): 1658 Problem Description XOR is a kind of bit operator, we

*HDU2852 树状数组(求第K小的数)

KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3864    Accepted Submission(s): 1715 Problem Description For the k-th number, we all should be very familiar with it. Of course,to

菲波那契数列,求第k个数的值

题目:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子成长到第三个月后每个月又生一对兔子,假如兔子不死,问每个月兔子的总数是多少? 1 //菲波那契数列,输出第k个数的值 2 #define _CRT_SECURE_NO_WARNINGS 3 #include <stdio.h> 4 5 int FibonacciNum(int n){ 6 if (n < 0){ 7 return -1; 8 } 9 if (n == 0){ 10 return 0; 11 } 12 else