【习题 5-14 UVA - 1598】Exchange
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
各组数据之间有空行!
且最后一行后面没有空行!
然后就是用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的更多相关文章
- Uva - 1598 - Exchange
本来想用优先队列做,可是不知道怎么处理之间的关系,最后还是用了map方法AC了,不过速度上有些慢,提交的时候跑了1.557秒.估计这道题时间都稍微长些,题目的时间限制也是4.5秒,不像一般题目的3秒限 ...
- SICP 习题 (1.14)解题总结
SICP 习题 1.14要求计算出过程count-change的增长阶.count-change是书中1.2.2节讲解的用于计算零钱找换方案的过程. 要解答习题1.14,首先你需要理解count-ch ...
- UVA Foreign Exchange
Foreign Exchange Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Your non ...
- 武汉科技大学ACM :1008: 华科版C语言程序设计教程(第二版)习题6.14
Problem Description 输入一个八进制的字符串,将它转换成等价的十进制字符串,用pringf的%s格式输出. Input 首先输入一个正整数t,表示有t组测试数据(1<= t & ...
- Java50道经典习题-程序14 求日期
题目:输入某年某月某日,判断这一天是这一年的第几天?分析:(1)以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 (2)特殊情况,闰年2月份的天数是29天,否则是28天 impo ...
- SQL表操作习题4 14~25题 缺20题
- 【习题5-4 UVA-10763】Foreign Exchange
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果x>y 则num[(x,y)]--; 否则num[(x,y)]++; 看看每一个二元组的num值是不是都为0就好. [代码 ...
- [C++] 习题 2.14 用队列实现桶排序
目录 前置技能 队列(已在上篇提到栈的时候顺便提到了,不再赘述) 桶排序 具体实现 由用户输入n个10以内的数,每输入i(0≤i≤9),就把它插入第i号队列中,最后把10个队列中的非空队列,按队列号从 ...
- shell习题第14题:
[题目要求] 需求,根据web服务器的访问日志,把一些请求高的ip给拒绝掉,并且每隔半小时把不再发起请求或者请求量很小的ip给解封 假设: 1. 一分钟内请求量高于100次的ip视为不正常的请求 2. ...
随机推荐
- express中的中间件理解
什么是中间件 中间件是一个可访问请求对象(req)和响应对象(res)的函数,在 Express 应用的请求-响应循环里,下一个内联的中间件通常用变量 next 表示.中间件的功能包括: 执行任何代码 ...
- Android ijkplayer详解使用教程
1.认识ijkplayer 最近公司准备开发一款视频播放及直播的应用,找了许多开源的框架,大部分都是基于ffmpeg开发的.最开始准备用Vitamio框架开发的,相关的文章也比较丰富,结果对于非个人移 ...
- 前端js中this指向及改变this指向的方法
js中this指向是一个难点,花了很长时间来整理和学习相关的知识点. 一. this this是JS中的关键字, 它始终指向了一个对象, this是一个指针; 参考博文: JavaScript函数中的 ...
- 去除inline-block元素间距
- 【Codeforces Round #428 (Div. 2) A】Arya and Bran
[Link]: [Description] [Solution] 傻逼题 [NumberOf WA] [Reviw] [Code] #include <bits/stdc++.h> usi ...
- C++怎么访问私有变量和函数
用指针呀,了解C++内存结构的话. 1. 对于私有成员变量,可以用指针来访问. 2. 对于虚函数,也可以用指针来访问. 3. 另外,对于私有成员,如果摸不准地址构造,可以先构造一个结构相似的类,然后增 ...
- C语言之文件操作06——写数据到文本文件遇0停止
//文件 /* =============================================================== 题目:输入10个篮球运动员的身高数据(cm)保存至D盘文 ...
- zendiscovery 的Ping机制
ping是集群发现的基本手段,通过在网络上广播或者指定ping某些节点获取集群信息,从而可以找到集群的master加入集群.zenDiscovery实现了两种凭机制:广播与单播.本篇将详细分析一些这M ...
- POJ 1737 Connected Graph (大数+递推)
题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单(无重边无自环)连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS ...
- MFC CListctr显示缩略图
我们知道通过CImageList可以让listctr显示出图片,但是添加的图片大小必须和要CImageList 创建的图片大小一致,才能显示出来.最近遇到一个需求,需要把很多大小不一的jpeg图片通过 ...