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 ...
随机推荐
- 开发一个 app 有多难?
171 个回答 默认排序 道衍天机 有事情的加微信1293190838找我 1,150 人赞同了该回答 ----------------------------------------------- ...
- 一口一口吃掉Hibernate(五)——一对多单向关联映射
版权声明:本文为博主原创文章,未经博主允许不得转载.如需转载请声明:[转自 http://blog.csdn.net/xiaoxian8023 ] 在上一篇博客<一口一口吃掉Hibernate( ...
- 利用Python进行数据分析——Numpy基础:数组和矢量计算
利用Python进行数据分析--Numpy基础:数组和矢量计算 ndarry,一个具有矢量运算和复杂广播能力快速节省空间的多维数组 对整组数据进行快速运算的标准数学函数,无需for-loop 用于读写 ...
- 分别用face++和百度获取人脸属性(python单机版)
称之为单机版,主要是相对于调用摄像头实时识别而言.本篇主要py2下利用face++和百度接口获取本地图片中的人脸属性,并按照一定格式保存数据. face++版 face++是刚注册的,只能用一个试用的 ...
- Dynamics CRM 查找字段下拉的最多10个选项的排序规则
原文链接来自DTCCh论坛http://dynamics.ms-talent.com.cn/bbs/content/?id=1406&catogory=CRM 如果你是从事dynamics c ...
- 《An Introduction to Signal Smoothing》译文
最近在做数据平滑相关的工作,正好读到该篇博客,感觉不错,就翻译了一下.原链接:An Introduction to Signal Smoothing 信号平滑简介 噪声无处不在,不管是在采集手机游戏的 ...
- Linux Mint 17一周使用体验
1 Win7下安装Mint双系统 Linux Mint支持直接从Win7硬盘引导安装,非常方便,不用制作U盘引导,更不用刻盘安装了.Mint有Cinnamon和Mate两种桌面,听说Mate更加简洁节 ...
- 深入Java虚拟机(2)——Java的平台无关性
一.平台无关性的好处 Java技术在网络环境下非常有用,其中一个关键理由是,用Java创建的可执行二进制程序,能够不加改变地运行于多个平台. 这样的平台无关性随之带来许多的好处.这将极大地减轻系统管理 ...
- Android开发技巧——大图裁剪
本篇内容是接上篇<Android开发技巧--定制仿微信图片裁剪控件> 的,先简单介绍对上篇所封装的裁剪控件的使用,再详细说明如何使用它进行大图裁剪,包括对旋转图片的裁剪. 裁剪控件的简单使 ...
- Spring Boot 中应用Spring data mongdb
摘要 本文主要简单介绍下如何在Spring Boot 项目中使用Spring data mongdb.没有深入探究,仅供入门参考. 文末有代码链接 准备 安装mongodb 需要连接mongodb,所 ...