CF1028D Order book 思维

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 (1n3633041≤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 (1p3089830661≤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;
}

  

转载于:https://www.cnblogs.com/l609929321/p/9552585.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值