习题5-14 交易所(Exchange,ACM/ICPC NEERC 2006,UVa1598)

原题链接:https://vjudge.net/problem/UVA-1598
分类:STL综合
备注:复杂模拟,阅读理解
前言:在别处找到一篇很nice的翻译:https://blog.csdn.net/zju2016/article/details/75005098?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
顺便他还有题解,但是我搞不懂map的迭代器的用法,比如map.rbegin是指向哪里(我知道是begin的前面,但是不知道元素应该是什么),map元素的顺序是什么样的,我没弄懂,目前也没查到,就没抄他的。但是因为他的代码我知道这题必须要erase(在此之前我已经TLE很多次了!),当题目开始注重效率难度就上升了一小层啊…

代码如下:

#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn = 1e4 + 5;
const int inf = 99999;
int bnum, bpri, snum, spri;

struct COrder {
	string type;
	int num, pri, act = 1;
}ord[maxn];

set<int, greater<int> >buy;//记录buy的pri
set<int, less<int> >sell;//记录sell的pri
map<int, set<int> >buyxb;//记录同种pri的buy订单的下标
map<int, set<int> >sellxb;//记录同种pri的sell订单的下标
map<int, int>buy_tot;//buy价格映射总数
map<int, int>sell_tot;//sell价格映射总数

void trade(int xb, string x, int pri){
	if (x[0] == 'B') {
		if (pri < spri)return;
		for (set<int, less<int> >::iterator it = sell.begin(); it != sell.end();) {
			if (!ord[xb].act) break;
			for (set<int>::iterator it2 = sellxb[*it].begin(); it2 != sellxb[*it].end(); ++it2) {
				if (!ord[*it2].act)continue;
				if (ord[*it2].pri <= pri) {
					int ntmp = min(ord[xb].num, ord[*it2].num);
					ord[*it2].num -= ntmp; sell_tot[*it] -= ntmp;
					if (!ord[*it2].num)ord[*it2].act = 0;
					ord[xb].num -= ntmp; buy_tot[pri] -= ntmp;
					printf("TRADE %d %d\n", ntmp, ord[*it2].pri);
					if (!ord[xb].num) { ord[xb].act = 0; break; }
				}
			}
			if (!sell_tot[*it]) sell.erase(it++);
			else it++;
		}
	}
	else {
		if (pri > bpri)return;
		for (set<int,greater<int> >::iterator it = buy.begin(); it != buy.end(); ) {
			if (!ord[xb].act) break;
			for (set<int>::iterator it2 = buyxb[*it].begin(); it2 != buyxb[*it].end(); ++it2) {
				if (!ord[*it2].act)continue;
				if (ord[*it2].pri >= pri) {
					int ntmp = min(ord[xb].num, ord[*it2].num);
					ord[*it2].num -= ntmp; buy_tot[*it] -= ntmp;
					if (!ord[*it2].num)ord[*it2].act = 0;
					ord[xb].num -= ntmp; sell_tot[pri] -= ntmp;
					printf("TRADE %d %d\n", ntmp, ord[*it2].pri);
					if (!ord[xb].num) { ord[xb].act = 0; break; }
				}
			}
			if (!buy_tot[*it]) buy.erase(it++);
			else it++;
		}
	}
}

void output(){
	bnum = buy_tot.count(bpri) ? buy_tot[bpri] : 0, snum = sell_tot[spri], sell_tot.count(spri) ? sell_tot[spri] : 0;
	if (!bnum) {
		bpri = 0;
		for (set<int,greater<int> >::iterator it = buy.begin(); it != buy.end(); ++it) {
			if (buy_tot[*it]) {
				bpri = *it; bnum = buy_tot[*it]; break;
			}
		}
	}
	if (!snum) {
		spri = inf;
		for (set<int,less<int> >::iterator it = sell.begin(); it != sell.end(); ++it) {
			if (sell_tot[*it]) {
				spri = *it; snum = sell_tot[*it]; break;
			}
		}
	}
	printf("QUOTE %d %d - %d %d\n", bnum, bpri, snum, spri);
}

int main(void) {
	int n, flag = 0;
	while (~scanf("%d", &n)) {
		buy.clear(); sell.clear();
		buyxb.clear(); sellxb.clear();
		buy_tot.clear(); sell_tot.clear();
		if (flag)printf("\n");
		else flag = 1;
		bnum = 0, bpri = 0, snum = 0, spri = inf;
		string s;
		for (int i = 1; i <= n; i++) {
			cin >> s;
			if (s[0] == 'C') {
				int xb;
				scanf("%d", &xb);
				if (ord[xb].act) {
					ord[xb].act = 0;
					if (ord[xb].type[0] == 'B') {
						buy_tot[ord[xb].pri] -= ord[xb].num;
						if (!buy_tot[ord[xb].pri]) {
							set<int, greater<int> >::iterator it = buy.find(ord[xb].pri);
							if (it != buy.end())buy.erase(it);
						}
					}
					else {
						sell_tot[ord[xb].pri] -= ord[xb].num;
						if (!sell_tot[ord[xb].pri]) {
							set<int, greater<int> >::iterator it = sell.find(ord[xb].pri);
							if (it != sell.end())sell.erase(it);
						}
					}
				}
			}
			else
			{
				int num, pri; scanf("%d%d", &num, &pri);
				ord[i].type = s, ord[i].num = num, ord[i].pri = pri, ord[i].act = 1;
				if (s[0] == 'B') {
					buy.insert(pri); buyxb[pri].insert(i); buy_tot[pri] += num;
					if (pri > bpri) bpri = pri;
				}
				else {
					sell.insert(pri); sellxb[pri].insert(i); sell_tot[pri] += num;
					if (pri < spri)spri = pri;
				}
				trade(i, s, pri);
			}
			output();
		}
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值