Black Box --C ++

I, entitled

Black Box represents a primitive database.

It can be used to store an array of integers, and it has a special variable i.

In the beginning, the black box is empty, and i = 0.

Now black box series operation process, comprising the following two operations:

1, ADD (x): x represents added to the black box.
2, GET: i is incremented by 1, the output of the black box a small value (ie, all the i-th number of press after ascending order) of the i.

A specific example is given below:

序号 操作        i     盒子内数(升序排列后)             输出的值 
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   

For convenience of description, we define the following two sequences:

1, A (1), A (2), ..., A (M): This sequence is obtained from the addition of all the elements are arranged in a black box after the order of addition, A in the above example of the sequence (3,1, - 4,2,8, -1000,2).

2, u (1), u (2), ..., u (N): This item i is represented by the sequence number of a GET operation within the i-th element in the box. u sequence example is (1,2,6,6).

Now they're all values ​​obtained during the operation in accordance with the output given sequences A and u.

Input Format

Inputs include three lines.

The first line contains two integers M and N, the length of sequence A and sequence u.

The second line contains M integers, representing each element A sequence.

The third line contains N integers, each element u represents the sequence.

Separated by a space between each number of peers.

Output Format

All numerical GET operation output during the output operation.

Each value per line.

data range

| A (I) | <2 * 109 = | A (I) | <= 2 * 109,
1≤N≤M≤300001≤N≤M≤30000,
all pp (1≤p≤N1≤p≤N ), p≤u (p) ≤Mp≤u ( p) ≤M established

Sample input:

7 4
3 1 -4 2 8 -1000 2
1 2 6 6

Sample output:

3
3
1
2
Difficulty: Moderate
Time / space restrictions: 1s / 64MB
Total by: 113
The total number of attempts: 193
Source: "Advanced algorithm Competition Guide"

 

 

Second, the idea

This question is examined concept large pile top, by maintaining a small sequence data stack top and the large stack top (small or large heap root root pile), in the maintenance process, the search order is to pay attention to:

/ *
Steps of the algorithm ideas:
The first step: from the perspective of number theory to moderation, taking the form of its data, relationships, recursive formula.
Step two: Starting from the data structure point designed algorithm explanations steps, generally having a data structure and operation of the coupling (i.e., an algorithm structure and algorithm operating a correspondence relationship).
The third step: from programming, the realization of the program.
* /

/*

Sequential search algorithm: enumeration does not leak; storage state variables

*/

 

Third, to achieve


/*算法搜索中的三个问题:1.按什么样的顺序搜索2.怎样枚举才能不重不漏所有情况或变量3.变量或状态用数组还是多个变量存储*/
//算法的一般思路:考察对顶推,用两个堆;来维护一个序列。

#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>

using  namespace  std;
const int N=30010;
int  n,m; //n表示ADD操作次数,m表示get操作次数
int a[N],b[N];

int  main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)cin>>a[i];
    for(int j=0;j<m;j++)cin>>b[j];
    sort(b,b+m);
    priority_queue<int> left;
    priority_queue<int,vector<int>,greater<int>> right;
    /*题解思想:通过维护大顶堆、小顶堆,完成这个模拟操作操作*/
    //b[m]数组暗含了add(x)和get两个操作
    int i=0,j=0;  //枚举操作确定索引变量
    //程序设计
    while(i<n || j<m)  //设计:一步一步操作
    {
      while(j<m && b[j]==i)
        {
           //get操作  在i个元素情况下,get的操作。
           cout<<right.top()<<endl;
           left.push(right.top());
           right.pop();
           j++;
            
        }
        //add操作分两种情况
        int x=a[i];
        if(left.empty()||x>=right.top()) right.push(x);
        else
        {
            left.push(x);
            right.push(left.top());
            left.pop();
        }
        i++;//i表示数组a的索引号
    }
    return 0;
}

 

Published 21 original articles · won praise 8 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_40405758/article/details/99294994