【习题 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. ...
随机推荐
- javascript中易犯的错误有哪些
javascript中易犯的错误有哪些 一.总结 一句话总结: 比如循环中函数的使用 函数中this的指向谁(函数中this的使用) 变量的作用域 1.this.timer = setTimeout( ...
- Docker+Jenkins持续集成
Docker+Jenkins持续集成 使用etcd+confd实现容器服务注册与发现 前面我们已经通过jenkins+docker搭建了基本的持续集成环境,实现了服务的自动构建和部署,但是,我们遇 ...
- IDC机房KVM应用案例分析
IDC机房KVM应用案例分析 一.背景介绍 随着信息技术的发展,各行各业都在马不停蹄的开展着各自的信息化建设步伐.对于设计制造创新科技产品为运行主业的设计院而言,内部IT基础设备与机房管理结构的完善与 ...
- Kinect 开发 —— 骨骼追踪
骨骼追踪技术通过处理景深数据来建立人体各个关节的坐标,骨骼追踪能够确定人体的各个部分,如那部分是手,头部,以及身体.骨骼追踪产生X,Y,Z数据来确定这些骨骼点.骨骼追踪系统采用的景深图像处理技术使用更 ...
- centos的dns配置总结
找的一篇好的存根 DNS正反向解析 DNS(Domain Name Service)域名服务.就是域名解析服务器.所谓名称解析的过程就是某个应用程序基于某个搜索键在指定的数据库中查询.而后查询到某些对 ...
- mapper提示Could not autowire. No beans of … type found?
工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql 在自动生成工具生成代码后,serv ...
- maven 遇到failOnMissingWebXml有关问题解决方法
(转自) http://blog.csdn.net/liuvlun/article/details/50218507
- Linux 安装Nginx具体图解教程
系统:Centos6.6 64位 Nginx: http://nginx.org/en/download.html 眼下最新版本号1.9.4 我下载1.8.0 watermark/2/text/a ...
- javascript 获取HTML DOM父,子,临近节点
在Web应用程序特别是Web2.0程序开发中.常常要获取页面中某个元素,然后更新该元素的样式.内容等.怎样获取要更新的元素,是首先要解决的问题.令人欣慰的是,使用JavaScript获取节点的方法有非 ...
- poj 1001 java大精度
import java.io.* ; import java.math.* ; import java.util.* ; import java.text.* ; public class Main ...