Codeforces Educational Codeforces Round 27 - D - Driving Test

驾驶考试模拟
本文介绍了一个模拟驾驶考试场景的问题,通过一系列事件如改变车速、超车等,模拟驾驶员的行为,并计算出为了符合规定最少需要忽视多少个交通标志。
D. Driving Test
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. Polycarp changes the speed of his car to specified (this event comes with a positive integer number); 
  2. Polycarp's car overtakes the other car; 
  3. Polycarp's car goes past the "speed limit" sign (this sign comes with a positive integer); 
  4. Polycarp's car goes past the "overtake is allowed" sign; 
  5. Polycarp's car goes past the "no speed limit"; 
  6. 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
Note

In the first example Polycarp should say he didn't notice the "speed limit" sign with the limit of 70 and the second "speed limit" sign with the limit of 120.

In the second example Polycarp didn't make any rule violation.

In the third example Polycarp should say he didn't notice both "no overtake allowed" that came after "overtake is allowed" sign.


题意:1-6,6个数字分别代表六个事件:1.车速变为s、2.超车、3.接下来路段限速为s、4.允许超车、5.不限速、6.不允许超车,求最少有几个路牌没有看到


题解:可能导致违规有三个操作:1、超车,2、改变速度,3、新限速牌

因超车忽略的标示牌的个数就是在当前位置前到允许超车的标志牌间的不允许超车的标志牌个数,用cnt来记录,每次遇到不允许超车路牌cnt++否则cnt清零。

每次改变速度时要判断是否超过上一个路牌的速度限制,如果超过了则再判断上上个,直到没有限制路牌或路牌限制速度≥当前速度。该操作可以用栈来实现。

每次遇到新路牌要先与当前速度比较,再判断是否加入到栈。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stack>
using namespace std;
int n,t,s,speed,cnt,ans;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        speed = cnt = ans = 0;
        stack<int>st;
        while(!st.empty()) st.pop();
        for(int i=0;i<n;i++)
        {
            scanf("%d",&t);
            switch (t)
            {
                case 1://车速到s
                    scanf("%d",&s);
                    speed = s;
                    while(!st.empty()&&st.top()<speed)
                    {
                        ans++;
                        st.pop();
                    }
                    break;
                case 2://超车
                    ans+=cnt,cnt=0;
                    break;
                case 3://限速为s
                    scanf("%d",&s);
                    if(speed>s)
                        ans++;
                    else
                        st.push(s);
                    break;
                case 4://允许超车
                    cnt = 0;
                    break;
                case 5://无限速
                    while(!st.empty()) st.pop();
                    break;
                default://不允许超车
                    cnt++;
                    break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值