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 ...
随机推荐
- Win2003及2008R2重启自动登录设置方法
在windows系统中,使用最多的可能就是远程操作了,关于远程操作的那些事很多用户还是有些迷茫的.如果win2003系统远程重启后,要重新登录系统才能启用远程功能,这就十分的麻烦,如何才能实现重启后的 ...
- 如何为分布式系统优雅的更换RPC
为啥需要更换RPC? 很多小伙伴都遇到过需要为分布式系统调用更换RPC的问题,为什么会遇到这种事呢?其实,在系统搭建初期,需求简单,架构简单,最重要的是请求量也少,所以很多系统都采用快速原型开发模式, ...
- python webdriver环境搭建
一.准备安装包 1.下载python 2.下载setuptools 3.下载pip 二.windows环境安装 1.安装python,建议选择python2.7.5版本. 2.安装setuptools ...
- CNN中的经典结构之AlexNet
AlexNet的基本结构 Alexnet是由5个卷积层和三个全连接层组成,一共8个权重层(池化层不是权重层因为其没有参数),其中ReLU激活函数作用在每个卷积层和全连接层上,在第一个卷积层和第二个卷积 ...
- static class 静态类(Java)
一般情况下是不可以用static修饰类的.如果一定要用static修饰类的话,通常static修饰的是匿名内部类. 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static ...
- java常用的几种线程池比较
1. 为什么使用线程池 诸如 Web 服务器.数据库服务器.文件服务器或邮件服务器之类的许多服务器应用程序都面向处理来自某些远程来源的大量短小的任务.请求以某种方式到达服务器,这种方式可能是通过网络协 ...
- 项目管理软件系列-Linux一键安装禅道
linux用一键安装包 简介:本文介绍如何在linux下面使用禅道一键安装包搭建禅道的运行环境. linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅 ...
- Ubuntu16.04下安装jdk1.8过程
笔者环境:腾讯云服务器 Ubuntu16.04 x64 一 . 去oracle官网下载对应的jdk 下载地址:http://www.oracle.com/technetwork/java/javase ...
- jenkins + pipeline构建自动化部署
一.引言 Jenkins 2.x的精髓是Pipeline as Code,那为什么要用Pipeline呢?jenkins1.0也能实现自动化构建,但Pipeline能够将以前project中的配置信息 ...
- dict 字典的用法
因为这一章节用的比较多,而需要注意的得比较多, 所以总结的笔记也相对比较多一点,我就直接单独写了一页 ## {key:value} 1. key不能为列表 2.value可以是任意类型值 3.返 ...