本来想用优先队列做,可是不知道怎么处理之间的关系,最后还是用了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. Nginx+Tomca+Redis实现负载均衡、资源分离、session共享

    目标实现:Nginx作为负载均衡后端多Tomcat实例,通过Redis实现Session共享. 操作系统环境:CentOS 6.8 SSH:SecureCRT 其中 Nginx服务:80端口 Tomc ...

  2. Unity使用C++作为游戏逻辑脚本的研究

    文章申明:本文来自JacksonDunstan的博客系列文章内容摘取和翻译,版权归其所有,附上原文的链接,大家可以有空阅读原文:C++ Scripting( in Unity) 一.C#和C++的通信 ...

  3. URL重定向漏洞,python打造URL重定向漏洞检测脚本

    前言: 今天学习了重定向漏洞,这个漏洞比较好理解 漏洞名:URL重定向漏洞 威胁:低 漏洞的来源:开发者对head头做好对应的过滤和限制 例子: 有漏洞的网站:http://a.com/x.php?u ...

  4. Codeforces Round #396(Div. 2) A. Mahmoud and Longest Uncommon Subsequence

    [题意概述] 找两个字符串的最长不公共子串. [题目分析] 两个字符串的最长不公共子串就应该是其中一个字符串本身,那么判断两个字符串是否相等,如果相等,那么肯定没有公共子串,输出"-1&qu ...

  5. jQuery – AJAX get() 和 post() 方法

    jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据. HTTP 请求:GET vs. POST 两种在客户端和服务器端进行请求-响应的常用方 ...

  6. mybatis常用配置

    前面两篇博客我们简单介绍了mybatis的使用,但是在mybatis的配置问题上我们只是使用了最基础的配置,本文我们就来说说其他一些常用的配置.如果小伙伴对mybatis尚不了解,可以先参考这两篇博客 ...

  7. webpack 将不同类型的文件输出到不同文件夹

    参考:https://stackoverflow.com/questions/33058964/configure-webpack-to-output-images-fonts-in-a-separa ...

  8. vue以及js的一些坑或常用技巧

    判断空object Object.getOwnPropertyNames(obj).length === 0 模拟range Array.apply(null, Array(5)).map(funct ...

  9. linux TCP头部的构造的简单分析

    TCP的头部的构造是在函数tcp_transmit_skb()中进行的 函数片段如下: /* Build TCP header and checksum it. */ th = tcp_hdr(skb ...

  10. Linux 下的一个全新的性能测量和调式诊断工具 Systemtap, 第 2 部分: DTrace

    DTrace的原理本系列文章详细地介绍了一个 Linux 下的全新的调式.诊断和性能测量工具 Systemtap 和它所依赖的基础 kprobe 以及促使开发该工具的先驱 DTrace 并给出实际使用 ...