UVA 12266 Stock prices --优先队列
优先队列。
做法:维护两个优先队列: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 --优先队列的更多相关文章
- hdu 4163 Stock Prices 水
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #de ...
- hdu 4163 Stock Prices 花式排序
Stock Prices Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- UVA.136 Ugly Numbers (优先队列)
UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...
- uva 1422 - Processor(二分+优先队列)
题目链接:uva 1422 - Processor 题目大意:有一个机器要处理一些问题,给出这些问题可以开始的时间和必须完成的时间,以及任务的工作量,问说机器必须以最少每秒多少得工作量才能完成这些任务 ...
- UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 10954 Add All(优先队列)
题意 求把全部数加起来的最小代价 a+b的代价为(a+b) 越先运算的数 要被加的次数越多 所以每次相加的两个数都应该是剩下序列中最小的数 然后结果要放到序列中 也就是优先队列了 #inc ...
- uva 10537 Toll! Revisited(优先队列优化dijstra及变形)
Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...
- 【暑假】[深入动态规划]UVa 1412 Fund Management
UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS Memory Limit: Unknown ...
- 第3.2 使用案例1:股票期货stock portfolio 21050917
As mentioned earlier in the chapter, the first use case revolves around a stock portfolio use case ...
随机推荐
- 关于网络上的各种mysql性能测试结论
关于网上的各种性能测试帖子,我想说以下几点: 1.为了使性能测试更加的客观.实际,应该说明针对什么场景进行测试,查询.还是修改,是否包含了主键,包含了几个索引,各自的差别是什么.因为不同的mysql分 ...
- [转载]拜占庭问题深入讨论 from http://bitkan.com/news/topic/14011
拜占庭将军问题深入探讨 了解过比特币和区块链的人,多少都听说过拜占庭将军问题,或听说过比特币(或区块链)的一个重要成就正是解决了拜占庭将军问题.但真正明白这个问题的人并不多,甚至知道这个问题实质的人都 ...
- 基于流的自动化构建工具------gulp (简单配置)
项目上线也有一阵子,回头过来看了看从最初的项目配置到开发的过程,总有些感慨,疲软期,正好花点时间,看看最初的配置情况 随着前端的发展,前端工程化慢慢成为业内的主流方式,项目开发的各种构建工具,也出现了 ...
- 关于SharePoint 的Client object model该何时load和execut query的一点自己的看法
很多人在用client object model的时候,不知道何时或者该不该load,今天看到一个观点描述这个问题,觉得很有道理,和大家分享.那就是写client object model就像写sql ...
- wxPython简单入门
wxPython简介 wxPython 是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的.功能键全的 GUI 用户界面. wxPython 是作为优秀 ...
- 二叉查找树(binary search tree)详解
二叉查找树(Binary Search Tree),也称二叉排序树(binary sorted tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树上所有结点的值均小于 ...
- 求当前时间100天后的时间日期,格式化为xxxx年xx月xx日
package com.demo1; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Da ...
- .NET下单文件的上传处理
ASP.NET的单文件上传使用控件 <asp:FileUpload ID="upmess" runat="server" Width="248p ...
- IOS之UI -- UITableView -- 1 -- 相关初识
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Xcode 插件失效的临时解决方案
每当Xcode升级之后,都会导致原有的Xcode插件不能使用,这是因为每个插件的Info.plist中记录了该插件兼容的Xcode版本的DVTPlugInCompatibilityUUID,而每个版本 ...