Order book
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. Somebody adds a new order of some direction with some price.
  2. 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:

  1. "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.
  2. "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.

Input

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

Output the number of ways to restore directions of ADD actions modulo 109+7109+7.

Examples
input

Copy
6
ADD 1
ACCEPT 1
ADD 2
ACCEPT 2
ADD 3
ACCEPT 3
output

Copy
8
input

Copy
4
ADD 1
ADD 2
ADD 3
ACCEPT 2
output

Copy
2
input

Copy
7
ADD 1
ADD 2
ADD 3
ADD 4
ADD 5
ACCEPT 3
ACCEPT 5
output

Copy
0
Note

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 思维的更多相关文章

  1. 集合划分——cf1028D思维题

    非常思维的一道题目,题意很长 给定s1,s2两个集合,s1维护最大值,s2维护最小值,s1的所有元素要比s2小 操作1:往两个集合里的任意一个添加x 操作2:把x从所在的集合里删掉:要求被删的x必须是 ...

  2. "Becoming Functional" 阅读笔记+思维导图

    <Becoming Functional>是O'Reilly公司今年(2014)7月发布的一本薄薄的小册子,151页,介绍了函数式编程的基本概念.全书使用代码范例都是基于JVM的编程语言, ...

  3. MySql学习(二) —— where / having / group by / order by / limit 简单查询

    注:该MySql系列博客仅为个人学习笔记. 这篇博客主要记录sql的五种子句查询语法! 一个重要的概念:将字段当做变量看,无论是条件,还是函数,或者查出来的字段. select五种子句 where 条 ...

  4. mybatis的#{}和${}的区别以及order by注入问题

    前言略,直奔主题.. #{}相当于jdbc中的preparedstatement ${}是输出变量的值 你可能说不明所以,不要紧我们看2段代码: String sql = "select * ...

  5. 【JAVAWEB学习笔记】09_MySQL多表&JDBC(包含MySQL数据库思维导图)

    今天晨读单词: order:订单constraint:(强制)约束foreign key:外键references:指向orderitem:订单项join:加入resourceBundle:资源捆绑c ...

  6. 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 ...

  7. vue源码逐行注释分析+40多m的vue源码程序流程图思维导图 (diff部分待后续更新)

    vue源码业余时间差不多看了一年,以前在网上找帖子,发现很多帖子很零散,都是一部分一部分说,断章的很多,所以自己下定决定一行行看,经过自己坚持与努力,现在基本看完了,差ddf那部分,因为考虑到自己要换 ...

  8. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  9. 二叉树的锯齿形层次遍历 · Binary Tree Zigzag Level Order Traversal

    [抄题]: 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) [思维问题]: 不知道反复切换要怎么做:用boolean normalOrder当作布尔型 ...

随机推荐

  1. scroll-苹果滑动卡顿

    2018年08月02日,程序小bug. 在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 一行代码搞定: - ...

  2. .Net Core DevOps -免费用Azure四步实现自动化发布(CI/CD)

    前言 linux 大行其道的今天想必大家都已经拥抱 core 了吧,通常的方案都是 gitlab+jenkins+centos,但是这样的方案不适合我这种懒人,一直在寻求简单的解决方案,在寻求方案的过 ...

  3. js中判断一个对象的类型的种种方法

    javascript中检测对象的类型的运算符有:typeof.constructor.instanceof. typeof:typeof是一个一元运算符,返回结果是一个说明运算数类型的字符串.如:&q ...

  4. Go基础语法学习

    Go语言基础 Go是一门类似C的编译型语言,但是它的编译速度非常快.这门语言的关键字总共也就二十五个,比英文字母还少一个,这对于我们的学习来说就简单了很多.先让我们看一眼这些关键字都长什么样: 下面列 ...

  5. 使用DOM4J 对xml解析操作

    参考自:https://blog.csdn.net/redarmy_chen/article/details/12969219 dom4j是一个Java的XML API,类似于jdom,用来读写XML ...

  6. Windows下的bat原来可以为我们做很多

    用了windows系统这么多年了,对bat也不是很了解.最近研究了一下bat的用法.这里就大概列举一下自己的用法 参考网址 基本命令 echo echo我们可以理解成程序中的输出,和我们Java的Sy ...

  7. 神奇的 SQL 之温柔的陷阱 → 三值逻辑 与 NULL !

    前言 开心一刻   一个中国小孩参加国外的脱口秀节目,因为语言不通,于是找了一个翻译. 主持人问:“Who is your favorite singer ?” 翻译:”你最喜欢哪个歌手啊 ?” 小孩 ...

  8. CodeForces 29D Ant on the Tree

    洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译. 这题有\(\bm3\)种解法,但只有一种是正解(这不是废话嘛). 方法\(\bm1\):最近公共祖先LCA(正 ...

  9. 重学计算机组成原理(六)- 函数调用怎么突然Stack Overflow了!

    用Google搜异常信息,肯定都访问过Stack Overflow网站 全球最大的程序员问答网站,名字来自于一个常见的报错,就是栈溢出(stack overflow) 从函数调用开始,在计算机指令层面 ...

  10. (四)c#Winform自定义控件-选择按钮组

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...