优先队列。

做法:维护两个优先队列:quesell  和  quebuy, 一个是小值优先,一个是大值优先。每次push的时候,都取各自的Top元素,比较价格,如果卖的比卖的出价低,则成交,各自的要买和要卖的股票数量减少能够减少的最大值,此时的DP(DealPrice)被记录下来。

具体见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <functional>
using namespace std;
#define N 100003 struct SELL
{
int price,num;
bool operator <(const SELL &a)const
{
return price>a.price;
}
}; struct BUY
{
int price,num;
bool operator <(const BUY &a)const
{
return price<a.price;
}
}; int main()
{
int t,n,i;
char ss[],share[],at[];
int num,price;
SELL ka;
BUY kb;
scanf("%d",&t);
int DP;
while(t--)
{
priority_queue<SELL> quesell;
priority_queue<BUY> quebuy;
DP = -;
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s%d%s%s%d",ss,&num,share,at,&price);
if(ss[] == 'b')
{
kb.price = price;
kb.num = num;
quebuy.push(kb);
}
else
{
ka.price = price;
ka.num = num;
quesell.push(ka);
}
int minnum;
if(!quesell.empty() && !quebuy.empty())
{
while()
{
if(quesell.empty() || quebuy.empty())
break;
BUY li = quebuy.top();
SELL wi = quesell.top();
if(wi.price <= li.price)
{
quebuy.pop();
quesell.pop();
if(wi.num > li.num)
{
wi.num -= li.num;
quesell.push(wi);
}
else if(wi.num < li.num)
{
li.num -= wi.num;
quebuy.push(li);
}
DP = wi.price;
}
else
break;
}
if(!quesell.empty())
printf("%d ",quesell.top().price);
else
printf("- ");
if(!quebuy.empty())
printf("%d ",quebuy.top().price);
else
printf("- ");
if(DP == -)
printf("-\n");
else
printf("%d\n",DP);
}
else
{
if(!quesell.empty())
printf("%d ",quesell.top().price);
else
printf("- ");
if(!quebuy.empty())
printf("%d ",quebuy.top().price);
else
printf("- ");
if(DP == -)
printf("-\n");
else
printf("%d\n",DP);
}
}
}
return ;
}

UVA 12266 Stock prices --优先队列的更多相关文章

  1. hdu 4163 Stock Prices 水

    #include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #de ...

  2. hdu 4163 Stock Prices 花式排序

    Stock Prices Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  3. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  4. uva 1422 - Processor(二分+优先队列)

    题目链接:uva 1422 - Processor 题目大意:有一个机器要处理一些问题,给出这些问题可以开始的时间和必须完成的时间,以及任务的工作量,问说机器必须以最少每秒多少得工作量才能完成这些任务 ...

  5. UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. UVa 10954 Add All(优先队列)

    题意  求把全部数加起来的最小代价  a+b的代价为(a+b) 越先运算的数  要被加的次数越多  所以每次相加的两个数都应该是剩下序列中最小的数  然后结果要放到序列中  也就是优先队列了 #inc ...

  7. uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

    Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...

  8. 【暑假】[深入动态规划]UVa 1412 Fund Management

    UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS   Memory Limit: Unknown   ...

  9. 第3.2 使用案例1:股票期货stock portfolio 21050917

    As mentioned earlier in the chapter, the first use case revolves around a stock portfolio use case  ...

随机推荐

  1. 【poj 3167】Cow Patterns(字符串--KMP匹配+数据结构--树状数组)

    题意:给2个数字序列 a 和 b ,问按从小到达排序后,a中的哪些子串与b的名次匹配. a 的长度 N≤100,000,b的长度 M≤25,000,数字的大小 K≤25. 解法:[思考]1.X 暴力. ...

  2. sqlite3之基本操作(一)

    简单的介绍 SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说没有独立的维护进程,所有的维护都来自于程序本身.它是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经 ...

  3. 打印机问题win7 和xp

    服务器端问题,重启如下服务 net stop "print spooler" net start "print spooler" gpedit.msc 本地计算 ...

  4. 【GOF23设计模式】组合模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_组合模式.树状结构.杀毒软件架构.JUnite底层架构.常见开发场景 package com.test.composite ...

  5. javascript小知识点

    大家对input中的value值研究的透彻么,今天看到一篇博客,很神奇  然后研究了一下input中的value值到底对应的是啥值 1.input中的value,这是大家在开发中进场遇到的一个问题 & ...

  6. js的基本数据类型有哪些?

    js的基本数据类型有哪些? ECMAScript中有5中简单数据类型(也称为基本数据类型): Undefined.Null.Boolean.Number和String.还有1中复杂的数据类型----O ...

  7. Python基础(7)--函数

    本篇文章将介绍如何将语句组织成函数,以及参数概念以及在程序中的用途 本文地址:http://www.cnblogs.com/archimedes/p/python-function.html,转载请注 ...

  8. IOS 杂笔-10(Base64 加密)

    base64加密是可逆的,因此并不是很安全,在一些注重安全的地方很少用到,但是在普通传输中可以使用. 切忌,base64加密是不安全的. // // ViewController.m // CX-Ba ...

  9. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

  10. 使用ObjectOutputStream进行socket通信的时候出现固定读到四个字节乱码的问题

    问题描述: 最近在写一个通信相关的项目,服务器端和客户端通过socket进行通信.本来想利用read的阻塞特性,服务器端和客户端按照一定的流程进行文件读写.结果发现客户端或者服务器read方法一直都返 ...