习题5-14(uva-1598)

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>

#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;

const int MAXSIZE = 1e4 + 7;
struct Node
{
	int price;
	int cnt;
	int id;
	int type;
	Node() {}

	Node(int a, int b,int i,int t) :price(a),cnt(b),id(i),type(t){}

	inline friend bool operator<(const Node& a, const Node& b) {
		if (a.price != b.price) return a.price < b.price;
		return a.id > b.id;
	}
	inline friend bool operator>(const Node& a, const Node& b) {
		if (a.price != b.price) return a.price > b.price;
		return a.id > b.id;
	}
};
char vis[MAXSIZE];
priority_queue<Node> buy;
priority_queue<Node, vector<Node>, greater<Node>> sell;

Node orders[MAXSIZE];
map<int, int> buyCnt;
map<int, int > sellCnt;

void Init()
{
	memset(vis, 0, sizeof(vis));

	while (!buy.empty()) buy.pop();
	while (!sell.empty()) sell.pop();

	buyCnt.clear();sellCnt.clear();

	buyCnt[0] = sellCnt[99999] = 0;

	buy.push(Node(0,0, MAXSIZE, 0));
	sell.push(Node(99999,0, MAXSIZE, 0));
}

int CheckCancel() {
	int ret = 0;
	while (buy.size() > 1 && vis[buy.top().id]) { ret = 1; buy.pop(); }
	while (sell.size() > 1 && vis[sell.top().id]) { ret = 1; sell.pop();}
	return ret;
}

void DoDeal(int type) {

	while (buy.top().price>= sell.top().price)
	{
		if (buy.size() <= 1 || sell.size() <= 1) break;

		if(CheckCancel()) continue;

		Node &a = orders[buy.top().id], &b = orders[sell.top().id];
		buy.pop(); sell.pop();

		int cut = min(a.cnt, b.cnt);

		a.cnt -= cut; buyCnt[a.price] -= cut;
		b.cnt -= cut; sellCnt[b.price] -= cut;

		if (a.cnt) buy.push(a);
		if (b.cnt) sell.push(b);

		if (type == 1) cout << "TRADE " << cut << " " << b.price << endl;
		else if(type == 2) cout << "TRADE " << cut << " " << a.price << endl;
	}
	CheckCancel();
}
int main()
{
	int n,t = 0;
	string temp;

	while (cin >> n && n) {

		if (t++) cout << endl;
		
		int p, c, id, cnt = 1,type = 0;	

		Init();

		while (n--) {

			cin >> temp;

			orders[cnt].type = 0;

			if (temp.front() == 'B' || temp.front() == 'S') {

				cin >> c >> p;

				orders[cnt] = Node(p, c, cnt, 1);

				if (temp.front() == 'B') {

					buy.push(orders[cnt]);

					buyCnt[p] += c;
				}
				else {
					orders[cnt].type = 2;

					sell.push(orders[cnt]);

					sellCnt[p] += c;
				}
			}
			else {
				cin >> id;
				if (!vis[id]) {
					if (orders[id].type == 1) {
						buyCnt[orders[id].price] -= orders[id].cnt;
					}
					else if (orders[id].type == 2)
						sellCnt[orders[id].price] -= orders[id].cnt;
				}
				vis[id] = 1;
			}

			DoDeal(orders[cnt].type);

			cout << "QUOTE " << buyCnt[buy.top().price] << " " << buy.top().price << " - " << sellCnt[sell.top().price] << " " << sell.top().price << endl;
			
			cnt++;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值