Bank’s customer queue [Simple use of Set and Pair in STL]

answer:

If you sort it every time, it will definitely time out, you can use the set automatically sorted in STL, because it is two numbers, so you can add pair.

If it is 2, then select one at the end and delete it.

If it is 3, select one from the beginning and delete it.

Adding pair sorting means first sorting by the first row, and then by the second, all in descending order.

Pay attention to the begin() and end() of set.

The naive thoughts at the beginning:

At first, I thought of using two priority queues, one large and one small, and then open two maps, one to record the priority value, and one record whether to delete the number. In theory, it can be ok hahaha, but later discovered This is very troublesome, and I gave up after writing the meeting. Some examples are not easy to handle.

Just review the use of set and pair.

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int q,k,p;
    set<pair<int ,int > > st;
    set<pair<int, int> > :: iterator it;
    while(cin >> q && q){
        if(q == 1){
            cin >> k >> p;
            st.insert(make_pair(p,k));
        }
        else if(q == 2){
            if(st.empty()){
                cout << 0 << endl;
            }
            else {
                it = st.end();
                it--;
                cout << it->second << endl;
                st.erase(it);
            }
        }
        else if(q == 3){
            if(st.empty()){
                cout << 0 << endl;
            }
            else {
                it = st.begin();
                cout << it->second << endl;
                st.erase(it);
            }
        }
    }
    return 0;    
}

 

A certain bank is very arrogant and has some customers. Sometimes the customers with the highest priority are received first, and sometimes the customers with the lowest priority are received first. There are four operations as follows:

0: The system stops service.

1 K P: Add a customer whose ID is K(K\leq 10^6), and its priority is P(P\leq 10^7).

2: Query the customer with the highest priority, receive him, and delete it from the waiting queue.

3: Query the customer with the lowest priority, receive him, and delete it from the waiting queue.

Your task is to output the IDs of these customers in turn.

Input format

Several lines, ending with 0 (the total number of operations does not exceed 10^5).

A client may visit multiple times; it is guaranteed that the priority in the queue is different at any time.

Output format

For operations 2 and 3, an integer in a row represents D. If the query has no results, 0 is output.

 

Guess you like

Origin blog.csdn.net/Mercury_Lc/article/details/106451785