[C++] Solution with Two Priority_queues
Idea:\nThis problem is like a trading system. With a bunch of sell orders, when a buying order comes in, the system would match the buying order with the sellin
Idea:\nThis problem is like a trading system. With a bunch of sell orders, when a buying order comes in, the system would match the buying order with the selling orders whose prices is less than or equal to the buying prices. Same thing goes with the buy orders. In order to find which one is the best match, or there is no match at all, we can use the priority_queue(heap) to simulate the process.\n\nCode:\n```\nclass Solution {\npublic:\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n        priority_queue<pair<int,int>, vector<pair<int,int>>, BuyComp> buy;\n        priority_queue<pair<int,int>, vector<pair<int,int>>, SellComp> sell;\n        long res = 0;\n        \n        for (vector<int>& order : orders) {\n            int sum = order[1];\n            // buy order\n            if (order.back() == 0) {\n                while (!sell.empty() && sell.top().first <= order[0] && order[1]) {\n                    int amount = min(order[1], sell.top().second);\n                    if (sell.top().second == amount) {\n                        sell.pop();\n                    } else {\n                        pair<int,int> cur = sell.top();\n                        sell.pop();\n                        cur.second -= amount;\n                        sell.push(cur);\n                    }\n                    order[1] -= amount;\n                }\n                if (order[1] > 0) {\n                    buy.push(make_pair(order[0], order[1]));\n                }\n\t\t\t// sell order\n            } else {\n                while (!buy.empty() && buy.top().first >= order[0] && order[1]) {\n                    int amount = min(order[1], buy.top().second);\n                    if (buy.top().second == amount) {\n                        buy.pop();\n                    } else {\n                        pair<int,int> cur = buy.top();\n                        buy.pop();\n                        cur.second -= amount;\n                        buy.push(cur);\n                    }\n                    order[1] -= amount;\n                }\n                if (order[1] > 0) {\n                    sell.push(make_pair(order[0], order[1]));\n                }\n            }\n            res += order[1]*2 - sum;\n        }\n        return res%mod;\n    }\nprivate:\n    const int mod = 1e9+7;\n    \n    // price, amount\n    struct BuyComp {\n        bool operator() (pair<int,int>& a, pair<int,int>& b) {\n            return a.first < b.first;\n        }\n    };\n    \n    struct SellComp {\n        bool operator() (pair<int,int>& a, pair<int,int>& b) {\n            return a.first > b.first;\n        }\n    };\n};\n```
['C', 'Heap (Priority Queue)']