本来想用优先队列做,可是不知道怎么处理之间的关系,最后还是用了map方法AC了,不过速度上有些慢,提交的时候跑了1.557秒。估计这道题时间都稍微长些,题目的时间限制也是4.5秒,不像一般题目的3秒限制。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert> 

using namespace std;

struct CMD {
	string cmd;
	int size, price;
	CMD(string kind, int x = 0, int y = 0) :cmd(kind), size(x), price(y) {}
};

map<int, set<int> > BUY, SELL;
map<int, int> BUY_VAL, SELL_VAL;
vector<CMD> D;

void trade(int kind)
{
	while (!BUY.empty() && !SELL.empty()) {
		if (BUY.rbegin()->first >= SELL.begin()->first) {
			set<int> &v1 = BUY.rbegin()->second;
			set<int> &v2 = SELL.begin()->second;
			int aid = *v1.begin(), bid = *v2.begin();
			int z = min(D[aid].size, D[bid].size);
			printf("TRADE %d %d\n", z, kind ? D[aid].price : D[bid].price);
			D[aid].size -= z, D[bid].size -= z;
			BUY_VAL[D[aid].price] -= z, SELL_VAL[D[bid].price] -= z;
			if (D[aid].size == 0)
				v1.erase(aid);
			if (D[bid].size == 0)
				v2.erase(bid);
			if (v1.size() == 0)
				BUY.erase(D[aid].price);
			if (v2.size() == 0)
				SELL.erase(D[bid].price);
		}
		else {
			return;
		}
	}
}

// 打印QUOTE一行
void print()
{
	while (BUY_VAL.size() && BUY_VAL.rbegin()->second == 0) {
		BUY_VAL.erase(BUY_VAL.rbegin()->first);
	}
	while (SELL_VAL.size() && SELL_VAL.begin()->second == 0) {
		SELL_VAL.erase(SELL_VAL.begin()->first);
	}
	printf("QUOTE ");
	if (BUY_VAL.size()) {
		printf("%d %d", BUY_VAL.rbegin()->second, BUY_VAL.rbegin()->first);
	}
	else {
		printf("0 0");
	}
	printf(" - ");
	if (SELL_VAL.size()) {
		printf("%d %d", SELL_VAL.begin()->second, SELL_VAL.begin()->first);
	}
	else {
		printf("0 99999");
	}
	cout << endl;
}

int main()
{
	int Q, cases = 0;
	char cmd[16];
	while (scanf("%d", &Q) == 1) {
		if (cases++)
		{
			cout << endl;
		}
		// 记得清空
		BUY.clear(), SELL.clear();
		BUY_VAL.clear(), SELL_VAL.clear();
		D.clear();
		int size, price, id;
		// 读入命令并处理交易
		for (int i = 0; i < Q; i++) {
			scanf("%s", cmd);
			if (!strcmp(cmd, "BUY")) {
				scanf("%d %d", &size, &price);
				BUY[price].insert(i);
				BUY_VAL[price] += size;
				D.push_back(CMD("BUY", size, price));
				trade(0);
			}
			else if (!strcmp(cmd, "SELL")) {
				scanf("%d %d", &size, &price);
				SELL[price].insert(i);
				SELL_VAL[price] += size;
				D.push_back(CMD("SELL", size, price));
				trade(1);
			}
			else if (!strcmp(cmd, "CANCEL")) {
				scanf("%d", &id), id--;
				D.push_back(CMD("CANCEL", id));
				if (D[id].cmd == "BUY") {
					BUY[D[id].price].erase(id);
					if (BUY[D[id].price].size() == 0)
						BUY.erase(D[id].price);
					BUY_VAL[D[id].price] -= D[id].size;
					D[id].size = 0;
				}
				if (D[id].cmd == "SELL") {
					SELL[D[id].price].erase(id);
					if (SELL[D[id].price].size() == 0)
						SELL.erase(D[id].price);
					SELL_VAL[D[id].price] -= D[id].size;
					D[id].size = 0;
				}
			}
			print();
		}
	}
	return 0;
}

Uva - 1598 - Exchange的更多相关文章

  1. 【习题 5-14 UVA - 1598】Exchange

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 各组数据之间有空行! 且最后一行后面没有空行! 然后就是用set来模拟就好. 删除的时候,不着急删除. 因为并不用时刻输出集合大小. ...

  2. UVA Foreign Exchange

    Foreign Exchange Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Your non ...

  3. UVA 10763 Foreign Exchange 出国交换 pair+map

    题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...

  4. UVA 10763 Foreign Exchange

      Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description Your non- ...

  5. uva 10763 Foreign Exchange <"map" ,vector>

    Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthu ...

  6. uva 10763 Foreign Exchange(排序比较)

    题目连接:10763 Foreign Exchange 题目大意:给出交换学生的原先国家和所去的国家,交换成功的条件是如果A国给B国一个学生,对应的B国也必须给A国一个学生,否则就是交换失败. 解题思 ...

  7. UVa 10763 Foreign Exchange(map)

    Your non-profitorganization (iCORE - international Confederationof Revolver Enthusiasts) coordinates ...

  8. uva:10763 - Foreign Exchange(排序)

    题目:10763 - Foreign Exchange 题目大意:给出每一个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换.要求每一位同学都能找到和他交换的交换生. 解题思 ...

  9. Foreign Exchange UVA - 10763

      Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordin ...

随机推荐

  1. js原生获取元素的css属性

    习惯了用jQuery的css()的方法获取元素的css属性,突然不用jquery了,当要获得元素的css时候,我瞬间停顿了一下,咦?咋获取元素的css值?比如获取元素的width.是这样么?docum ...

  2. angular 路由的引用

    使用angular路由 遇到的坑. 使用cmd 安装好node.js 安装成功 输入node  -v 能查看版本说明安装成功 搭建angular项目输入命令 npm install  -g  angu ...

  3. C# 基础问答

    1.静态变量和非静态变量的区别? 2.const 和 static readonly 区别? 3.extern 是什么意思? 4.abstract 是什么意思? 5.internal 修饰符起什么作用 ...

  4. PyChram简单使用教程

    一.PyChram下载官网:http://www.jetbrains.com/pycharm Windows:http://www.jetbrains.com/pycharm/download/#se ...

  5. [JCIP笔记](五)JDK并发包

    这一节来讲一讲java.util.concurrent这个包里的一些重要的线程安全有关类. synchronized容器 synchronized容器就是把自己的内部状态封装起来,通过把每一个publ ...

  6. MySQL DELETE 语句

    MySQL DELETE 语句 你可以使用 SQL 的 DELETE FROM 命令来删除 MySQL 数据表中的记录. 你可以在mysql>命令提示符或PHP脚本中执行该命令. 语法 以下是S ...

  7. 实验与作业(Python)-04 数据类型、数学函数与字符串

    截止日期 实验目标 继续熟悉for循环与turtle 数值运算符 math库常用函数 字符串转化为数值类型(int, float, complex) 字符串常用函数 实验内容 任务1.使用for代替w ...

  8. 《读书报告 -- Elasticsearch入门 》--简单使用(2)

    <读书报告 – Elasticsearch入门 > ' 第四章 分布式文件存储 这章的主要内容是理解数据如何在分布式系统中存储. 4.1 路由文档到分片 创建一个新文档时,它是如何确定应该 ...

  9. A discussion of Dead Connection Detection, Resource Limits, V$SESSION, V$PROCESS and OS processes

    A discussion of Dead Connection Detection, Resource Limits, V$SESSION, V$PROCESS and OS processes (文 ...

  10. 一个未排序整数数组,有正负数,重新排列使负数排在正数前面,并且要求不改变原来的正负数之间相对顺序,比如: input: 1,7,-5,9,-12,15 ans: -5,-12,1,7,9,15 要求时

    #include <iostream> using namespace std; void txsort(int* arr, int len) { if (!arr || len == 1 ...