Luo Gu P5375 [[THUPC2019] combination of data structures Problem problem solution

The title looks very high theory, but in fact is not so complicated. Only you need to simulate the process queue, stack, heap and rootlets large root stack, respectively.

Details and comments in the code.

$ \rm code $

# The pragma the GCC Optimize ( 2 ) 
# the include <bits / STDC ++ H.>
 The using  namespace STD; 
Queue < int > que; 
Stack < int > STK; 
The priority_queue < int > Heap1; 
The priority_queue < int , Vector < int >, Greater < int >> Heap2;
 // use of C ++ STL library, four data structures are defined: the queue, stack, heap and rootlets large root stack 
int main () { 
    iOS :: sync_with_stdio ( 0 ), cin.tie ( 0 );
     int N;bool flag[5];
    memset(flag, true, sizeof(flag));
    cin >> N;
    while(N--) {
        int opt, x;
        cin >> opt >> x;
        if(opt == 1) {
            if(flag[1]) que.push(x);
            if(flag[2]) stk.push(x);
            if(flag[3]) heap1.push(x);
            if(flag[4]) heap2.push(x);
            //If the black box may temporarily be this data structure and if opt is 1, then insert X
             // . Inserting operation is simulated here 
        }
         the else {
             IF (que.empty ()) In Flag [ 1 ] = to false ;
             IF (STK. empty ()) In Flag [ 2 ] = to false ;
             IF (heap1.empty ()) In Flag [ . 3 ] = to false ;
             IF (heap2.empty ()) In Flag [ . 4 ] = to false ;
             // if the data structure is already empty , the change directly to the flag 
            IF (flag [ . 1 ]) {
                 IF (que.front ()! = X) flag [1] = false;
                que.pop();
            }
            if(flag[2]) {
                if(stk.top() != x) flag[2] = false;
                stk.pop();
            }
            if(flag[3]) {
                if(heap1.top() != x) flag[3] = false;
                heap1.pop();
            }
            if(flag[4]) {
                if(heap2.top() != x) flag[4] = false;
                heap2.pop (); 
            } 
            // analog deletion. 
        } 
    } 
    for ( int I = . 1 ; I <= . 4 ; ++ I) 
        In Flag [I] the puts (? " Yes " ): the puts ( " No " ); // output 
    return  0 ; 
}

 

Reproduced in: https: //www.cnblogs.com/Xray-luogu/p/11006400.html

Guess you like

Origin blog.csdn.net/weixin_34185320/article/details/93306831