P1801 Black Box [stack]

Title Description

Black Box is a primitive database. It can store an array of integers, and a special variable i. The very beginning of Black Box is empty. And i is equal to 0. The Black Box to deal with a string of commands.

Only two commands:

ADD (x): x elements into the BlackBox;

GET: i plus 1, then the output Blackhox in small numbers of i.

Remember: the i-th smaller number is the number of Black Box in ascending sort order after a large i-th element. E.g:

We have to demonstrate what a command 11 command string. (As shown below)

img

Now asked to find the best treatment for a given command string. ADD and GET command up to 200,000 respectively. Now represented by two array of integers command string:

1.A (1), A (2), ... A (M): a series of elements to be placed in the Black Box. Each number is no more than the absolute value of the integer 2 billion, M $ 200000. For example, the above example is A = (3,1, a 4,2,8, -1000,2).

2.u (1), u (2), ... u (N): represents u (j) elements are placed inside the Black Box after the emergence of a GET command. For example, in the above example u = (l, 2,6,6). Input data is not wrong ruling.

Resolve

I saw, dynamic maintenance of the k small, this is not a template on top of the heap right.

Simple talk about: For a decreasing sequence, length \ (n-\) , where the value can be divided into two parts: The first \ (1 \ sim k \) a large part of the number, the \ (k + 1 \ sim n-\) large numbers of the second portion. On top of the heap, stack maintenance is to use the number of two parts. We may wish to use a large root heap maintenance \ (1 \ the SIM k \) , with a small root heap maintenance \ (k + 1 \ the SIM the n-\) , so the first \ (k \) small numbers and \ (k + 1 \) small numbers are just the top of the heap, while the output of our output large root heap top of the heap on the line. This is why this method is called on the top of the heap.

Simple and quick to implement priority queues on it.

Reference Code

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define N 200010
using namespace std;
inline int read()
{
    int f=1,x=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
priority_queue<ll> Q;
priority_queue<ll,vector<ll>,greater<ll> > q;
int k,a[N],b[N],n,m;
int main()
{
    m=read(),n=read();
    for(int i=1;i<=m;++i) a[i]=read();
    for(int j=1;j<=n;++j) b[j]=read();
    k=1;
    Q.push(a[1]);
    for(int i=1;i<=m;++i){
        if(i>1){
            if(a[i]>Q.top()) q.push(a[i]);
            else Q.push(a[i]);
        }
        while(i==b[k]){
            while(Q.size()>k) q.push(Q.top()),Q.pop();
            while(Q.size()<k) Q.push(q.top()),q.pop();
            ++k,printf("%d\n",Q.top());
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/11518453.html