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. MapReduce 编程模型 & WordCount 示例

    学习大数据接触到的第一个编程思想 MapReduce.   前言 之前在学习大数据的时候,很多东西很零散的做了一些笔记,但是都没有好好去整理它们,这篇文章也是对之前的笔记的整理,或者叫输出吧.一来是加 ...

  2. javascript+jQuery补充

    一.jQuery事件绑定 <div class='c1'> <div> <div class='title'>菜单一</div> <div cla ...

  3. 激活函数、正向传播、反向传播及softmax分类器,一篇就够了!

    1. 深度学习有哪些应用 图像:图像识别.物体识别.图片美化.图片修复.目标检测. 自然语言处理:机器创作.个性化推荐.文本分类.翻译.自动纠错.情感分析. 数值预测.量化交易 2. 什么是神经网络 ...

  4. Hadoop 系列(八)—— 基于 ZooKeeper 搭建 Hadoop 高可用集群

    一.高可用简介 Hadoop 高可用 (High Availability) 分为 HDFS 高可用和 YARN 高可用,两者的实现基本类似,但 HDFS NameNode 对数据存储及其一致性的要求 ...

  5. 用jquery实现放大镜效果

    ----css代码--- *{margin:0;padding:0;} .showimg{position:relative;width:450px;height:420px;border:1px s ...

  6. 使用python画3D线条

    """用于验证整体趋势正确性""" #!python3 #-*- coding:utf-8 -*- import matplotlib as ...

  7. 2月9日 《Java 8实战》读后感

    第一部分 基础知识 第3章 Lambda表达式 使用函数式接口 Predicate Consumer Function 第二部分 函数式数据处理 第4章 引入流 第5章 使用流 第6章 用流收集数据 ...

  8. Java虚拟机(二)-对象创建

    这一篇大致说明一下,对象在Java堆中对象分配.内存布局以及访问定位 1.对象的创建 虚拟机在遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引 ...

  9. 浅谈IDEA集成SSM框架(SpringMVC+Spring+MyBatis)

    前言 学习完MyBatis,Spring,SpringMVC之后,我们需要做的就是将这三者联系起来,Spring实现业务对象管理,Spring MVC负责请求的转发和视图管理, MyBatis作为数据 ...

  10. 洛谷 P1960 列队

    题意简述 有一个n × m 的矩阵,第i行第j列元素编号为(i - 1)× m +j 每次将一个数取出,其他元素依次向左,向上填补空缺,最后将取出的数放入矩阵最后一格 求每次取出数的编号 题解思路 由 ...