Uva - 1598 - Exchange
本来想用优先队列做,可是不知道怎么处理之间的关系,最后还是用了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的更多相关文章
- 【习题 5-14 UVA - 1598】Exchange
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 各组数据之间有空行! 且最后一行后面没有空行! 然后就是用set来模拟就好. 删除的时候,不着急删除. 因为并不用时刻输出集合大小. ...
- UVA Foreign Exchange
Foreign Exchange Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Your non ...
- UVA 10763 Foreign Exchange 出国交换 pair+map
题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...
- UVA 10763 Foreign Exchange
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Your non- ...
- uva 10763 Foreign Exchange <"map" ,vector>
Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthu ...
- uva 10763 Foreign Exchange(排序比较)
题目连接:10763 Foreign Exchange 题目大意:给出交换学生的原先国家和所去的国家,交换成功的条件是如果A国给B国一个学生,对应的B国也必须给A国一个学生,否则就是交换失败. 解题思 ...
- UVa 10763 Foreign Exchange(map)
Your non-profitorganization (iCORE - international Confederationof Revolver Enthusiasts) coordinates ...
- uva:10763 - Foreign Exchange(排序)
题目:10763 - Foreign Exchange 题目大意:给出每一个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换.要求每一位同学都能找到和他交换的交换生. 解题思 ...
- Foreign Exchange UVA - 10763
Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordin ...
随机推荐
- SpringMVC中url-pattern /和/*的区别
http://blog.csdn.net/u010648555/article/details/51612030
- You And Me 不见不散!
泰戈尔说: 有一个夜晚,我烧毁了所有的记忆, 从此我的梦就透明了: 有个早晨我扔掉了所有的昨天, 从此我的脚步就轻盈了! 越过山丘,才发现无人等候! 有段话最近很流行:20多岁的你,迷茫又着急,你想要 ...
- Just for mysql
mysql的下载与安装 由于学校开设了数据库专业,并且最近准备在做一个web端的设计,虽然本人是负责前端(当然,前端技术也很LOW),但因种种原因,准备开始学习数据库相关的知识,以mysql为例. 昨 ...
- SSH(struts2+hibernate+spring)总结
1 前三个文章 是我对ssh的具体实现 虽然没有真的写一个ssh的例子出来 但是 意思应该传达到了 主要还是注解注入的ssh太模块化了 感觉写出来意义不大 个人水平有限 说不清 2 我一开是写的是st ...
- Hibernate QBC 条件查询(Criteria Queries) and Demos
目录 创建一个Criteria 实例 限制结果集内容 结果集排序 关联 动态关联抓取 查询示例 投影Projections聚合aggregation和分组grouping 离线detached查询和子 ...
- Linux系统中安装Oracle过程记录
第一章 安装数据库软件 1.1 修改密码及创建目录和权限 创建oracle用户和组 创建相关目录并赋权 1.2 设置oracle用户环境变量 ORACLE_BASE:产品基目录 ORACLE_HOME ...
- Luogu P3740 [HAOI2014]贴海报_线段树
线段树版的海报 实际上这个与普通的线段树相差不大,只是貌似数据太水,暴力都可以过啊 本来以为要离散的,结果没打就A了 #include<iostream> #include<cstd ...
- 验证码识别之w3cschool字符图片验证码(easy级别)
起因: 最近在练习解析验证码,看到了这个网站的验证码比较简单,于是就拿来解析一下攒攒经验值,并无任何冒犯之意... 验证码所在网页: https://www.w3cschool.cn/checkmph ...
- golang 线程与通道
首先我们来看线程,在golang里面也叫goroutine 在读这篇文章之前,我们需要了解一下并发与并行.golang的线程是一种并发机制,而不是并行.它们之间的区别大家可以上网搜一下,网上有很多的介 ...
- 安全框架Shiro
原文地址:https://www.cnblogs.com/learnhow/p/5694876.html 一.架构 要学习如何使用Shiro必须先从它的架构谈起,作为一款安全框架Shiro的设计相当精 ...