【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

各组数据之间有空行!
且最后一行后面没有空行!
然后就是用set来模拟就好。
删除的时候,不着急删除。
因为并不用时刻输出集合大小。所以只要遇到了把它删掉就Ok.
把相同的合并那里。我直接暴力合并了。
因为
150 30
100 30
不能看出一个整体的250 30的。。
要分步输出Trade信息的.
然后在合并的时候也要注意看里面有没有已经删除了的。
已经删除了的就直接跳过。。

【代码】

#include <bits/stdc++.h>
using namespace std; const int N = 1e4; struct abc {
int num, price; abc(int x = 0, int y = 0) :num(x), price(y) {}
}; abc re[N + 10]; struct setcmp1
{
bool operator () (const int &a, const int &b)
{
if (re[a].price == re[b].price)
return a < b;
else
return re[a].price > re[b].price;
}
};
struct setcmp2
{
bool operator () (const int &a, const int &b)
{
if (re[a].price == re[b].price)
return a < b;
else
return re[a].price < re[b].price;
}
}; set <int, setcmp1> buyset;//买的价格,越大越好
set <int, setcmp2> sellset;//卖的价格,越小越好
bool dele[N + 10];//用来记录某个交易是否被删除了。并不用时刻输出集合大小。所以只要遇到了把它删掉就Ok
int n; void cl()
{
bool shan1, shan2;
do
{
shan1 = shan2 = false;
while (buyset.size() > 1 && dele[*buyset.begin()]) buyset.erase(buyset.begin()), shan1 = true;
while (sellset.size() > 1 && dele[*sellset.begin()]) sellset.erase(sellset.begin()), shan2 = true;
} while (shan1 || shan2);
} void out()
{
int num1 = 0, num2 = 0;
int price1 = re[*buyset.begin()].price, price2 = re[*sellset.begin()].price;
for (auto it : buyset)
if (re[it].price == price1)
{
if (!dele[it]) num1 += re[it].num;
}
else break;
for (auto it : sellset)
if (re[it].price == price2)
{
if (!dele[it]) num2 += re[it].num;
}
else break;
printf("QUOTE %d %d - %d %d\n", num1, price1, num2, price2);
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
int kk = 0;
while (~scanf("%d", &n))
{
if (kk > 0) puts("");
kk++;
re[N + 1].num = 0, re[N + 1].price = 0;
re[N + 2].num = 0, re[N + 2].price = 99999;
memset(dele, 0, sizeof dele);
buyset.clear(); sellset.clear();
buyset.insert(N + 1); sellset.insert(N + 2);
for (int i = 1; i <= n; i++)
{
char s[10];
scanf("%s", s);
switch (s[0])
{
case ('C'):
{
int x;
scanf("%d", &x);
dele[x] = true;
cl();//看看队首是不是要删掉
out();
break;
}
case ('B'):
{//买进
int num, price;
scanf("%d%d", &num, &price);
while (sellset.size() > 1 && num >0 && price >= re[*sellset.begin()].price)
{
int temp = min(re[*sellset.begin()].num, num), temp2 = re[*sellset.begin()].price;
num -= temp;
re[*sellset.begin()].num -= temp;
if (re[*sellset.begin()].num == 0)
{
sellset.erase(sellset.begin());
cl();
}
printf("TRADE %d %d\n", temp, temp2);
}
re[i] = abc(num, price);
if (num != 0) buyset.insert(i);
out();
break;
}
case 'S':
{
//卖
int num, price;
scanf("%d%d", &num, &price);
while (buyset.size() > 1 && num >0 && price <= re[*buyset.begin()].price)
{
int temp = min(re[*buyset.begin()].num, num), temp2 = re[*buyset.begin()].price;
num -= temp;
re[*buyset.begin()].num -= temp;
if (re[*buyset.begin()].num == 0)
{
buyset.erase(buyset.begin());
cl();
}
printf("TRADE %d %d\n", temp, temp2);
}
re[i] = abc(num, price);
if (num != 0) sellset.insert(i);
out();
break;
}
default:
break;
}
}
}
return 0;
}

【习题 5-14 UVA - 1598】Exchange的更多相关文章

  1. Uva - 1598 - Exchange

    本来想用优先队列做,可是不知道怎么处理之间的关系,最后还是用了map方法AC了,不过速度上有些慢,提交的时候跑了1.557秒.估计这道题时间都稍微长些,题目的时间限制也是4.5秒,不像一般题目的3秒限 ...

  2. SICP 习题 (1.14)解题总结

    SICP 习题 1.14要求计算出过程count-change的增长阶.count-change是书中1.2.2节讲解的用于计算零钱找换方案的过程. 要解答习题1.14,首先你需要理解count-ch ...

  3. UVA Foreign Exchange

    Foreign Exchange Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Your non ...

  4. 武汉科技大学ACM :1008: 华科版C语言程序设计教程(第二版)习题6.14

    Problem Description 输入一个八进制的字符串,将它转换成等价的十进制字符串,用pringf的%s格式输出. Input 首先输入一个正整数t,表示有t组测试数据(1<= t & ...

  5. Java50道经典习题-程序14 求日期

    题目:输入某年某月某日,判断这一天是这一年的第几天?分析:(1)以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 (2)特殊情况,闰年2月份的天数是29天,否则是28天 impo ...

  6. SQL表操作习题4 14~25题 缺20题

  7. 【习题5-4 UVA-10763】Foreign Exchange

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果x>y 则num[(x,y)]--; 否则num[(x,y)]++; 看看每一个二元组的num值是不是都为0就好. [代码 ...

  8. [C++] 习题 2.14 用队列实现桶排序

    目录 前置技能 队列(已在上篇提到栈的时候顺便提到了,不再赘述) 桶排序 具体实现 由用户输入n个10以内的数,每输入i(0≤i≤9),就把它插入第i号队列中,最后把10个队列中的非空队列,按队列号从 ...

  9. shell习题第14题:

    [题目要求] 需求,根据web服务器的访问日志,把一些请求高的ip给拒绝掉,并且每隔半小时把不再发起请求或者请求量很小的ip给解封 假设: 1. 一分钟内请求量高于100次的ip视为不正常的请求 2. ...

随机推荐

  1. 考满分软件测试工程师(实习)面试&软达启航面试

    考满分软件测试工程师(实习)面试 从这学期秋季开学的时候开始准备找工作,一边学习看书,一边完善简历海投:九月下旬的时候在年级实习群里看到考满分发的宣传海报马上就加了hr的微信,hr要了我的简历,并给技 ...

  2. c/c++中static与extern关键字介绍

    一.C语言中的static关键字 在C语言中,static可以用来修饰局部变量,全局变量以及函数.在不同的情况下static的作用不尽相同. (1)修饰局部变量 一般情况下,对于局部变量是存放在栈区的 ...

  3. POJ 2374 线段树建图+Dijkstra

    题意: 思路: 线段树+Dijkstra(要堆优化的) 线段树要支持打标记 一个栅栏 拆成两个点 :左和右 新加一个栅栏的时候 看看左端点有没有被覆盖过 如果有的话 就分别从覆盖的那条线段的左右向当前 ...

  4. jq实现回车键执行方法

    $(function(){ $(document).keypress(function (e){ if(e.keyCode == 13){ //执行你想执行的方法,keyCode代表不同的按键 } } ...

  5. [Redux-Observable && Unit Testing] Use tests to verify updates to the Redux store (rxjs scheduler)

    In certain situations, you care more about the final state of the redux store than you do about the ...

  6. java / C++ B+树代码

    C++ 代码 #include <> JAVA 代码 package org.test.me.struct; /** * author: shuly * create: 2018-09-1 ...

  7. VPS搭建与IPv6使用教程

    VPS搭建与IPv6使用教程 SoftEther命令: yum -y install gcc zlib-devel openssl-devel readline-devel ncurses-devel ...

  8. js---26组合模式

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  9. 16.REPL 命令

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html ctrl + c - 退出当前终端. ctrl + c 按下两次 - 退出 Node REPL ...

  10. 给VG增加磁盘,给文件目录增加空间

    一: #lspv 找到新增加的物理卷(逻辑驱动器,以hdisk8为例). #chdev –l hdisk8 –a pv=yes写入新的物理卷的pvid. #extendvg cwdatavg hdis ...