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 ...
随机推荐
- Python函数:一个简单的迭代
#!/usr/bin/env python # -*- coding: utf-8 -*- def fact(n): if n == 1 : return 1 return n * fact(n-1) ...
- linux环形buff模拟多线程信号量操作
互斥锁mutex变量的值非0即1,只能用来表示两种状态下的临界资源.而信号量是与之类似的,用来表示可用资源的,区别在于,信号量可以表示多个可用资源的. --值为2的信号量也就是特殊的互斥锁了. 那么下 ...
- winform(多窗体、菜单和工具栏)
一.多窗体 1.哪个是主窗体 利用From1的button将From2打开 private void button1_Click(object sender, EventArgs e) { Form2 ...
- 跟踪js文件作为iframe页面不起作用时(淘宝天猫)
跟踪文件 (function(win, doc) { var s = doc.createElement("script"), h = doc.getElementsByTagNa ...
- UIMenuController的使用
1, 基本使用 以对一个UILabel长按弹出菜单为例 子类化UILabel 因为需要覆盖这几个方法:- (BOOL)canBecomeFirstResponder; 返回YES 同时需要在每次UI元 ...
- 一句命令快速合并 JS、CSS
在项目开发环境下,我们会把 JS 代码尽可能模块化,方便管理和修改,这就避免不了会出现一个项目自身 JS 文件数量达到 10 个或者更多. 而项目上线后,会要求将所有 JS 文件合并为 1 个或者几个 ...
- 请用fontAwesome代替网页icon小图标
1. 引言 网页小图标到处可见,如果一个网页都是干巴巴的文字和图片,而没有小图标,会显得非常简陋.下面的小图标,你是不是会经常用到? 你可能说——“我们用的都是彩色的,不是黑白的”——别着急,下面会讲 ...
- Mysql之执行计划
1.explain分析sql语句 例如: EXPLAIN ) ORDER BY bi.`publish_time` 返回结果: 而今天检查的不是这条sql,远比这条复杂,不过也能反映情况了. (1 ...
- 安卓开发_慕课网_ViewPager实现Tab(App主界面)
学习内容来自“慕课网” 网站上一共有4种方法来实现APP主界面的TAB方法 这里学习第一种 ViewPager实现Tab 布局文件有7个, 主界面acitivity.layout <Linear ...
- HDFS简单入门
本文地址:http://www.cnblogs.com/archimedes/p/hadoop-simple.html,转载请注明源地址. 欢迎关注我的个人博客:www.wuyudong.com, 更 ...