优先队列。

做法:维护两个优先队列: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. 机器学习实战 - 读书笔记(06) – SVM支持向量机

    前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习笔记,这次是第6章:SVM 支持向量机. 支持向量机不是很好被理解,主要是因为里面涉及到了许多数学知 ...

  2. Verilog学习笔记设计和验证篇(二)...............同步有限状态机

    上图表示的就是数字电路设计中常用的时钟同步状态机的结构.其中共有四个部分产生下一状态的组合逻辑F.状态寄存器组.输出组合逻辑G.流水线输出寄存器组.如果状态寄存器组由n个寄存器组成,就可以记忆2^n个 ...

  3. 每个软件都自己把操作系统的host配置项加到内存中供频繁调用

    nginx的转发,http_pass 转发到一个域名passport.ab.cn 那具体是到哪台机器上. 其实可以通过/etc/hosts文件来配置的. 可以理解,这个hosts文件是操作系统级别的, ...

  4. XML的约束(dtd)

    DTD(Document Type Definition),文档类型定义,DTD文件应使用UTF-8或Unicode   1.XML中有多少个元素,就在dtd文件中写几个 <!ELEMENT&g ...

  5. EntityFramework4.1开发

    常见问题大概为这几个 一.ef4.1 codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题. 二.ef4.1 没有了edmx等复杂的东西 变得简单 干 ...

  6. C++map类型

    map是键-值对的集合,可以理解为关联数组,可以使用键作为下标来获取一个值 本文地址:http://www.cnblogs.com/archimedes/p/cpp-map.html,转载请注明源地址 ...

  7. 【读书笔记】iOS-GCD-API

    一,Dispatch Queue dispatch_async(queue, ^{ /* *想执行的任务 */ }); 其中queue分为两种: 1,Serial Dispatch Queue 等待现 ...

  8. OC中NSArray

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  9. IOS学习资源收集--关于动画的代码学习资源总汇(很棒的动画效果哦)

    目录大纲: 1.很棒的iOS加载动画. github网址:https://github.com/NghiaTranUIT/FeSpinner 游老师的译文blog:http://www.cnblogs ...

  10. IOS之--UI进阶--多控制器管理第一天

    01-项目中常见的文件(LaunchScreen) Xcode5 框架是苹果事先已经导入进去的.在项目的结构当中就能够看到导入的框架. Xcode6 会自动导入一些觉见的框架.在项目结构当中,看不到已 ...