CF1028D Order book 思维
2 seconds
256 megabytes
standard input
standard output
Let's consider a simplified version of order book of some stock. The order book is a list of orders (offers) from people that want to buy or sell one unit of the stock, each order is described by direction (BUY or SELL) and price.
At every moment of time, every SELL offer has higher price than every BUY offer.
In this problem no two ever existed orders will have the same price.
The lowest-price SELL order and the highest-price BUY order are called the best offers, marked with black frames on the picture below.
The presented order book says that someone wants to sell the product at price 1212 and it's the best SELL offer because the other two have higher prices. The best BUY offer has price 1010.
There are two possible actions in this orderbook:
- Somebody adds a new order of some direction with some price.
- Somebody accepts the best possible SELL or BUY offer (makes a deal). It's impossible to accept not the best SELL or BUY offer (to make a deal at worse price). After someone accepts the offer, it is removed from the orderbook forever.
It is allowed to add new BUY order only with prices less than the best SELL offer (if you want to buy stock for higher price, then instead of adding an order you should accept the best SELL offer). Similarly, one couldn't add a new SELL order with price less or equal to the bestBUY offer. For example, you can't add a new offer "SELL 2020" if there is already an offer "BUY 2020" or "BUY 2525" — in this case you just accept the best BUY offer.
You have a damaged order book log (in the beginning the are no orders in book). Every action has one of the two types:
- "ADD pp" denotes adding a new order with price pp and unknown direction. The order must not contradict with orders still not removed from the order book.
- "ACCEPT pp" denotes accepting an existing best offer with price pp and unknown direction.
The directions of all actions are lost. Information from the log isn't always enough to determine these directions. Count the number of ways to correctly restore all ADD action directions so that all the described conditions are satisfied at any moment. Since the answer could be large, output it modulo 109+7109+7. If it is impossible to correctly restore directions, then output 00.
The first line contains an integer nn (1≤n≤3633041≤n≤363304) — the number of actions in the log.
Each of the next nn lines contains a string "ACCEPT" or "ADD" and an integer pp (1≤p≤3089830661≤p≤308983066), describing an action type and price.
All ADD actions have different prices. For ACCEPT action it is guaranteed that the order with the same price has already been added but has not been accepted yet.
Output the number of ways to restore directions of ADD actions modulo 109+7109+7.
6
ADD 1
ACCEPT 1
ADD 2
ACCEPT 2
ADD 3
ACCEPT 3
8
4
ADD 1
ADD 2
ADD 3
ACCEPT 2
2
7
ADD 1
ADD 2
ADD 3
ADD 4
ADD 5
ACCEPT 3
ACCEPT 5
0
In the first example each of orders may be BUY or SELL.
In the second example the order with price 11 has to be BUY order, the order with the price 33 has to be SELL order.
题意:一开始可买卖的物品数量为0,可买卖的物品区间为无穷大,当遇到ADD时,增加一个可买卖的物品,在遇到ACCEPT时可选择买或者卖物品,但是只能对可买卖的区间内的物品进行买卖,在买卖一次物品后,可买卖区间边界变为区间内比该物品大和小的一个数
分析:按照题目意思直接模拟
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 10;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
const double pi = acos(-1.0);
int main() {
ll n, x, le = -1e9, ri = 1e9, ans = 1, res = 1;
//res:区间内可买卖数量减一,初始化1方便后面运算,ans:买卖方法数,le,ri:买卖区间边界
set<ll> s; //买卖区间内数的集合
set<ll>::iterator it;
cin >> n;
s.insert(-1e9), s.insert(1e9);
while( n -- ) {
string str;
cin >> str >> x;
if( str == "ADD" ) {
s.insert(x);
if( x >= le && x <= ri ) {
res ++;
}
} else {
s.insert(x);
if( x < le || x > ri ) {
ans = 0;
} else if( x != le && x != ri ){
ans = ans*2%mod;
}
s.erase(x);
res = 1; //可以买卖的区间内数为0,回到初始值1
it = s.lower_bound(x);
ri = *it, le = *(--it);
}
//debug(le), debug(ri), debug(ans);
}
cout << ans*res%mod << endl;
return 0;
}
CF1028D Order book 思维的更多相关文章
- 集合划分——cf1028D思维题
非常思维的一道题目,题意很长 给定s1,s2两个集合,s1维护最大值,s2维护最小值,s1的所有元素要比s2小 操作1:往两个集合里的任意一个添加x 操作2:把x从所在的集合里删掉:要求被删的x必须是 ...
- "Becoming Functional" 阅读笔记+思维导图
<Becoming Functional>是O'Reilly公司今年(2014)7月发布的一本薄薄的小册子,151页,介绍了函数式编程的基本概念.全书使用代码范例都是基于JVM的编程语言, ...
- MySql学习(二) —— where / having / group by / order by / limit 简单查询
注:该MySql系列博客仅为个人学习笔记. 这篇博客主要记录sql的五种子句查询语法! 一个重要的概念:将字段当做变量看,无论是条件,还是函数,或者查出来的字段. select五种子句 where 条 ...
- mybatis的#{}和${}的区别以及order by注入问题
前言略,直奔主题.. #{}相当于jdbc中的preparedstatement ${}是输出变量的值 你可能说不明所以,不要紧我们看2段代码: String sql = "select * ...
- 【JAVAWEB学习笔记】09_MySQL多表&JDBC(包含MySQL数据库思维导图)
今天晨读单词: order:订单constraint:(强制)约束foreign key:外键references:指向orderitem:订单项join:加入resourceBundle:资源捆绑c ...
- Codeforces Round #426 (Div. 2)【A.枚举,B.思维,C,二分+数学】
A. The Useless Toy time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- vue源码逐行注释分析+40多m的vue源码程序流程图思维导图 (diff部分待后续更新)
vue源码业余时间差不多看了一年,以前在网上找帖子,发现很多帖子很零散,都是一部分一部分说,断章的很多,所以自己下定决定一行行看,经过自己坚持与努力,现在基本看完了,差ddf那部分,因为考虑到自己要换 ...
- 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密
你真的了解字典(Dictionary)吗? 从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...
- 二叉树的锯齿形层次遍历 · Binary Tree Zigzag Level Order Traversal
[抄题]: 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) [思维问题]: 不知道反复切换要怎么做:用boolean normalOrder当作布尔型 ...
随机推荐
- asp.net ashx处理程序中switch case的替代方案总结
目录 1.用委托字典代替switch...case; 2.利用反射替代switch...case: 3.比较两种方案 4.其他方案 4.说明 5.参考 在开发 asp.net 项目中,通常使用一般处理 ...
- Mobile game forensics
My friend Carrie'd like to know "Garena 传说对决" violates any mobile risks such as insecure d ...
- 破解EFCore扩展Dll --- Z.EntityFramework.Extensions.EFCore
安装 Z.EntityFramework.Extensions.EFCore Install-Package Z.EntityFramework.Extensions.EFCore -Version ...
- C# Winfrom 自定义控件——带图片的TextBox
效果: 描述: 本来是想用GDI在左边画图片上去的,文本是居中对齐,如果文本是左对齐,文本会把图片遮住控件长这样: 但这样做,输入框在获取焦点时候,会把图片挡住就像这样: 输入完成之后图片就会显示完整 ...
- python3 编译安装
前言: Linux下大部分系统默认自带python2.x的版本,最常见的是python2.6或python2.7版本,默认的python被系统很多程序所依赖,比如centos下的yum就是python ...
- Windows上的Linux容器
翻译自:https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/linux-contai ...
- 通过jmeter发送webservice接口请求
1.webservice接口地址:http://ip:port/...?wsdl 2.接口数据类型:<cuxGmiChukuRmaTrxV><salesrepId xmlns:xsi ...
- ThreadLocal为什么会内存泄漏
1.首先看下ThreadLocal的原理图: 在ThreadLocal的生命周期中,都存在这些引用. 其中,实线代表强引用,虚线代表弱引用: 2.ThreadLocal的实现:每个Thread维护一个 ...
- 史上最全面 Android逆向培训之__实战(hook微信)
我的CSDN博客:https://blog.csdn.net/gfg156196 by--qihao 书接上文,上回说到了xposed,接下来就用一下,体验一下商业项目的赶脚…… 上一篇:史上最全 ...
- k8s学习笔记
9.deployment:声明式的升级应用 9.1.使用RC实现滚动升级 #kubectl rolling-update kubia-v1 kubia-v2 --image=luksa/kubia:v ...