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 ...
随机推荐
- sqlserver 判断字段是否为空字符串或者null
isnull(f.mzm,'')<>'' 不为null且不为‘’ not(f.mzm is null) 不为null
- 一口一口吃掉Hibernate(七)——继承映射
前几篇博文中讲到了常用的几种关联映射.其实hibernate中还有一种"省劲儿"的映射,那就是--"继承映射". 学了这么多的关系映射了,继承映射,从字面上也能 ...
- 数据结构之Treap
1. 概述 同splay tree一样,treap也是一个平衡二叉树,不过Treap会记录一个额外的数据,即优先级.Treap在以关键码构成二叉搜索树的同时,还按优先级来满足堆的性质.因而,Treap ...
- 使用gogs搭建git私有仓库
搭建gogs 我的机器环境:centos 7 1.安装git yum install git 2.安装mysql gogs的数据存在mysql中,需要安装一个mysql来存数据,当然也有其他的选择 ...
- SAS中常见的数组函数
SAS中常见的数组函数有: dim dimk hbound hboundk lbound lboundk 数组函数计萁数组的维数.上下界,有利于写出可移植的程序,数组函数包括:dim(x) 求数组x第 ...
- MySql配置文件模板
写在开篇: 这个mysql得配置文件my.cnf,是我现在环境里常用得,包含基础配置及一些优化,本来一直在我得有道笔记里记录着,之前一直没有写博客的习惯,最近刚开始注册博客,就将这些东西 ...
- Java中的内存分配
Java程序在运行时,需要在内存中分配空间,为了提高效率,就对空间进行了不同区域的划分,因为每一片区域否有特定的处理数据方式和内存管理方式. 1.栈存储局部变量 2.堆存储new出来的东西 3.方法区 ...
- ACM 最小公倍数
给定两个正整数,计算这两个数的最小公倍数. Input 输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.Output对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行. ...
- 这交互炸了(四) :一分钟让你拥有微信拖拽透明返回PhotoView
本文已授权微信公众号:鸿洋(hongyangAndroid)原创首发 <交互炸了>或许是一系列高端特效教程, 文中会介绍一些比较炫酷的特效,以及实现的思路.特效实现本身也许不会有太大的难度 ...
- Android碎裂的粒子效果
最近看到一段时间都没怎么更新文章了,一直在学习iOS相关内容.偶然间看到一个碎裂的粒子效果,觉得很有意思,就查了查,参考下网上的思路自己撸了个轮子. 好了,说了这么多,先看看效果吧~ 依惯例,先说下行 ...