本来想用优先队列做,可是不知道怎么处理之间的关系,最后还是用了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. java 左移 右移

    public class test{ public static void main(String[] args) { int m = 9; int n = m >> 3; int p = ...

  2. Windows上安装scapy

    1. 环境: (1) 操作系统:win7 .server2012 (2) Python版本:Python3.6-64bit (3) 依赖模块Npcap(推荐) 或WinPcap.   ps:从logo ...

  3. ThreadLocal基本原理及运用

    ThreadLocal提供本地线程变量.这个变量里面的值(通过get方法获取)是和其他线程分割开来的,变量的值只有当前线程能访问到,不像一般的类型比如Person,Student类型的变量,只要访问到 ...

  4. 修复 Ubuntu 14.04 的系统设置残缺问题

    sudo apt-get install ubuntu-desktop

  5. 120. Triangle(中等)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  6. net框架运行原理

    核心是CLR(通用语言运行时), c#或者其它各种语言编译原理:将原代码通过相对的编译器(语法检查原代码分析)生成IL代码托管(IL也称托管代码),最后得到一个托管模块,一个或多个托管模块组成程序集( ...

  7. python中input()和raw_input()的区别

    两者均是python的内置函数,通过读取控制台的输入与用户实现交互.raw_input:将所有输入作为字符串看待,不管用户输入什么类型的都会转变成字符串.                   raw的 ...

  8. How to Change Default Web ADI Upload Parameters for FlexField Import / Validation

    How to Change Default Web ADI Upload Parameters for FlexField Import / Validation (文档 ID 553345.1) 转 ...

  9. 20160226.CCPP体系详解(0036天)

    程序片段(01):01.多线程.c+02.多线程操作.c 内容概要:多线程 ///01.多线程.c #include <stdio.h> #include <stdlib.h> ...

  10. xlwt以格式生成xls文件

    参考: http://blog.sina.com.cn/s/blog_5357c0af01019gjo.html http://www.programcreek.com/python/example/ ...