• 【优先队列-求第Ki大的数】Black Box


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

     1 #include <iostream>
     2 #include <queue>
     3 #include <stdio.h>
     4 #define MAX 30010
     5 using namespace std;
     6 struct Node1
     7 {
     8     int S;
     9     friend bool operator <(Node1 a,Node1 b)
    10     {return a.S>b.S;}
    11 };
    12 struct Node2
    13 {
    14     int S;
    15     friend bool operator <(Node2 a,Node2 b)
    16     {return a.S<b.S;}
    17 };
    18 int main()
    19 {
    20     int N,M,i,j,k,Q,A,B,Sign,t=1;
    21     Node1 Num1;
    22     Node2 Num2;
    23     int NUM[MAX];
    24     priority_queue<Node1>ID1;/*小顶堆*/
    25     priority_queue<Node2>ID2;/*大顶堆*/
    26     while(scanf("%d%d",&N,&M)!=EOF)
    27     {
    28         for(i=0;i<N;i++)
    29         {
    30             scanf("%d",&NUM[i]);
    31         }
    32         Sign=1;j=0;
    33         for(i=0;i<M;i++)
    34         {
    35             scanf("%d",&Q);
    36             while(j<Q&&j<N)
    37             {   /*添加小顶堆元素*/
    38                 Num1.S=NUM[j++];
    39                 ID1.push(Num1);
    40             }
    41             for(k=ID2.size();k<Sign;k++)
    42             {   /*求第K小的数,保证大顶堆有K个数*/
    43                 Num2.S=ID1.top().S;ID1.pop();
    44                 ID2.push(Num2);
    45             }
    46 
    47             while(ID2.size()>0&&ID1.top().S<ID2.top().S)
    48             {   /*维护*/
    49                 Num1.S=ID2.top().S;ID2.pop();
    50                 ID1.push(Num1);
    51 
    52                 Num2.S=ID1.top().S;ID1.pop();
    53                 ID2.push(Num2);
    54             }
    55             printf("%d
    ",ID2.top().S);
    56             Sign++;
    57         }
    58     }
    59     return 0;
    60 }
    View Code
    转载请备注:
    **************************************
    * 作者: Wurq
    * 博客: https://www.cnblogs.com/Wurq/
    * Gitee: https://gitee.com/wurq
    **************************************
  • 相关阅读:
    在TextBrowser显示中,如何让最新的数据永远出现在第一行或者是在窗口的最后显示信息
    在Qtlabel中显示数字十六进制和十进制都可以
    实现 在子界面的button按下,在主界面的label显示。
    今天在Qt子界面中的Button,转到槽转不过去,报错Qt The class containing 'Ui::MainWindow' could not be found in...
    Qt串口接收使用多个LCD控件显示不同的数据
    Qt图标自定义
    Qt绘制动态曲线
    3.3.2Qt的按钮部件
    Mesh Profile (5)启动配置(配网)
    SiliconLabs EFR32BG 定时器输入捕获和脉宽调制
  • 原文地址:https://www.cnblogs.com/Wurq/p/4683380.html
Copyright © 2020-2023  润新知