UPC-2020 Spring Individual Training Game 4 (Newborn Field)-E Black Box (Double Stack, Opposite Top Stack)

Refer to the blog
incarnation of a whale on an isolated island o
phython96
problem description
Black Box is a primitive database. It can store an array of integers, as well as a special variable i. At the beginning, the Black Box was empty, and i was equal to 0. This Black Box has to process a series of commands.
There are only two commands:
ADD (x): put the x element into the Black Box;
GET: add 1 to i, and then output the i-th smallest number in the Black Box.
Remember: the i-th smallest number is the i-th element after the numbers in the Black Box are sorted from smallest to largest.
For example: Let's demonstrate a command string with 11 commands. (As shown in the figure below)
Serial number operation i Database output
1 ADD(3) 0 3
2 GET 13 3
3 ADD(1) 11, 3
4 GET 21,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,81
10 GET 4 -1000,-4,1,2,3,8 2
11 ADD(2) 4-1000,-4,1,2,2, 3,8

Now it is required to find out the best way to deal with a given command string. There are up to 200,000 ADD and GET commands respectively.

Now use two integer arrays to represent the command string:
1. A(1), A(2),...A(M): A string of elements to be put into the Black Box. Each number is an integer that absolutely does not exceed 2000000000, and M≤200000. For example, the above example is
A=(3,1,-4,2,8,-1000,2).
2. u(1), u(2),...u(N): Indicates that a GET command appears after the u(j)th element is placed in the Black Box. For example, u=(1,2,6,6) in the above example.
No need to make mistakes when entering data.

Enter the
first line, two integers, M, N.
In the second line, M integers represent A(1) …A(M).
In the third line, N integers represent u(1)...u(N).
Output
Output the output string of Black Box according to the command string, one number per line.
Sample input Copy
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
Sample output Copy
3
3
1
2
Reminder
For 30% of data, M≤10000;
for 50% of data, M≤100000
for 100% Data, M≤200000
problem analysis
Insert picture description hereincarnation of whales in an isolated island.
Here we use two heaps to maintain the K -th largest value in the interval. The
upper one is the small root heap and the
lower one is the big root heap.
If there are always k-1 elements in the big root heap, And all the elements in the small root pile are larger than those in the big root pile, then the top of the small root pile is the k-th largest element in the interval; the
maintenance process:
1. Keep adding numbers to the big root pile to make the big root pile The number of the number is k-1, if there is more, put the top of the pile in the small root pile
2. When the top of the small root pile has been visited once, k++, put the top of the small root pile back Big root pile, so that the elements in the big root pile always keep k-1

**

ACcode

**

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define  UpMing main
#define re register
#define Accept return 0;
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf =0x3f3f3f3f;
const ll  inf2 =0x3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const double eps=1e-7;
const ll mod = 20011021;
const int N =1e6+10;
const long double PI=3.1415926535897932384626433832795;
const long double e=2.71828182845904523536028747135266;
typedef pair<int,int>PII;
inline ll read() {
    
    
    ll  x=0;
    bool f=0;
    char ch=getchar();
    while (ch<'0'||'9'<ch)    f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return f?-x:x;
}
priority_queue<ll,vector<ll>, greater<ll> >heap;
priority_queue<ll,vector<ll>, greater<ll> >mi;
priority_queue<ll >ma;
ll n,m,a[maxn],b[maxn],c[maxn],vis[maxn];
set<ll>s;
set<ll>::iterator it;
int UpMing() {
    
    
    m=read();
    n=read();
    ll cnt=1,th=0;
    for(int i=1 ; i<=m ; i++) a[i]=read();
    for(int i=1 ; i<=n; i++) b[i]=read(),vis[b[i]]++;
    for(int i=1 ; i<=m ; i++) {
    
    
        ma.push(a[i]);
        if(ma.size()>th) {
    
    
            mi.push(ma.top());
            ma.pop();
        }
        while(vis[i]) {
    
    
    printf("%lld\n",mi.top());
            ma.push(mi.top());
            mi.pop();
            th++;
            cnt++;
            vis[i]--;
        }
    }
  Accept;
}

Guess you like

Origin blog.csdn.net/wmy0536/article/details/105316921