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

【题意】

在这里输入题意

【题解】

各组数据之间有空行!
且最后一行后面没有空行!
然后就是用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. Linq聚合函数使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. Icomparer和Icomparable集合排序

    c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可 ...

  3. 企业实战之部署Solarwinds Network八部众

    企业实战之部署Solarwinds Network 网管系统八部众 Orion Network Performance Monitor是全面的带宽性能监控和故障管理软件,能监控并收集来自路由器.交换机 ...

  4. MATLAB 最优化计算 (二)

    matlab 程序设计 1, for start:increment:end  若默认步长为1,则为 for start:end ———— end while  condition ————  end ...

  5. [ReasonML] Named & optional params

    // ::country is named param // ::country=?: which make it optional // because we make ::country=? op ...

  6. php课程 12-38 php的类的构造方法和析构方法怎么写

    php课程 12-38 php的类的构造方法和析构方法怎么写 一.总结 一句话总结:a.__construct(参数){},__destruct(){},b.如果类中的一个方法和类名相同,则该方法为构 ...

  7. GPU-Z:显卡体质、显卡各传感器实时状态的查看

    1. TechPowerUp GPU-Z:查看显卡体质 下载地址:Download TechPowerUp GPU-Z | techPowerUp 点击 bus interface 后的?进行显卡的体 ...

  8. 洛谷 P1755 斐波那契的拆分

    P1755 斐波那契的拆分 题目背景 无 题目描述 已知任意一个正整数都可以拆分为若干个斐波纳契数,现在,让你求出n的拆分方法 输入输出格式 输入格式: 一个数t,表示有t组数据 接下来t行,每行一个 ...

  9. 洛谷 P1610 鸿山洞的灯

    P1610 鸿山洞的灯 题目描述 已知n盏灯以及每盏灯的位置p[i],p[i]均不相等,两盏相邻的灯当小于dist时,若这个安全距离里面还有灯是亮着时,就可以关掉该盏灯,(即若第i-1盏与第i+1盏的 ...

  10. theme-windowAnimationStyle 动画设置

    对于windowAnimationStyle 的引用,目前自己发现的有两处 1.就是直接在Theme 中引用的,如下 <style name="Theme.Funui" pa ...