CodeForces - 845D Driving Test(模拟,栈)

8 篇文章 0 订阅
7 篇文章 0 订阅

Description

Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types.

speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign 
(cancel the action of the previous sign of this type);
overtake is allowed: this sign means that after some car meets it, it can overtake any other car;
no speed limit: this sign cancels speed limit if any (car can move with arbitrary speed after this sign);
no overtake allowed: some car can't overtake any other car after this sign.

Polycarp goes past the signs consequentially, each new sign cancels the action of all the previous signs of it’s kind (speed limit/overtake). It is possible that two or more “no overtake allowed” signs go one after another with zero “overtake is allowed” signs between them. It works with “no speed limit” and “overtake is allowed” signs as well.

In the beginning of the ride overtake is allowed and there is no speed limit.

You are given the sequence of events in chronological order — events which happened to Polycarp during the ride. There are events of following types:

Polycarp changes the speed of his car to specified (this event comes with a positive integer number);
Polycarp's car overtakes the other car;
Polycarp's car goes past the "speed limit" sign (this sign comes with a positive integer);
Polycarp's car goes past the "overtake is allowed" sign;
Polycarp's car goes past the "no speed limit";
Polycarp's car goes past the "no overtake allowed";

It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).

After the exam Polycarp can justify his rule violations by telling the driving instructor that he just didn’t notice some of the signs. What is the minimal number of signs Polycarp should say he didn’t notice, so that he would make no rule violations from his point of view?

Input

The first line contains one integer number n (1 ≤ n ≤ 2·105) — number of events.

Each of the next n lines starts with integer t (1 ≤ t ≤ 6) — the type of the event.

An integer s (1 ≤ s ≤ 300) follows in the query of the first and the third type (if it is the query of first type, then it’s new speed of Polycarp’s car, if it is the query of third type, then it’s new speed limit).

It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).

Output

Print the minimal number of road signs Polycarp should say he didn’t notice, so that he would make no rule violations from his point of view.

Examples

input
11
1 100
3 70
4
2
3 120
5
3 120
6
1 150
4
3 300
output
2
input
5
1 100
3 200
2
4
5
output
0
input
7
1 20
2
6
4
6
6
2
output
2

题目大意

有四种路标:
限速
允许超车
无限速
禁止超车
操作的标志数是:
1代表将车速改变到某个值
2代表超车
3代表车经过了限速的路标
4代表车经过了允许超车的路标
5代表车经过了无限速的路标
6代表车经过了禁止超车的路标
现在有n种操作,问驾驶员最少需要忽略多少个路标,他的行车才是不违反交通法则的,最初状态下无限速且可超车。

解题思路

容易发现操作2、4、6和1、3、5可以独立的进行判断,
246:标记驾驶员经过的禁止超车路标的数目,遇到允许超车就将标记置0,超车时结果加上标记数;
135:标记驾驶员经过的限速路标的状态(栈维护),在经过无限速路标时就取消这些标记(栈清空),在驾驶员改变速度为 v 时,结果应加上此前所有限度小于 v 的路标个数(取栈顶元素比较);经过限速路标时,若限速大于当前驾驶速度,结果直接加1,否则入栈维护状态。

代码实现

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
typedef long long ll;
#define maxn 107
#define INF 0x3f3f3f3f
const double PI=acos(-1.0);
const double epx=exp(1.0);
stack<int> sk;
int main()
{
    IO;
    int n,t;
    int ans=0;
    int speed=0,limitspeed=INF,cnt=0;
    cin>>n;
    while(n--)
    {
        cin>>t;
        if(t==2)
        {
            ans+=cnt;
            cnt=0;
        }
        else if(t==4)
            cnt=0;
        else if(t==6)
            cnt++;
        else if(t==1)
        {
            cin>>speed;
            while(!sk.empty()&&speed>sk.top())
            {
                sk.pop();
                ans++;
            }
        }
        else if(t==5)
        {
            limitspeed=INF;
            while(!sk.empty())
                sk.pop();
        }
        else if(t==3)
        {
            cin>>limitspeed;
            if(speed>limitspeed)
                ans++;
            else
                sk.push(limitspeed);
        }
    }
    cout<<ans<<endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值