Driving Test (Codeforces 845D)
Tags: Greedy, data structures

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.

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.

Sample Inputs:
11
1 100
3 70
4
2
3 120
5
3 120
6
1 150
4
3 300

5
1 100
3 200
2
4
5

7
1 20
2
6
4
6
6
2

Sample Outputs:
2

0

2

Solution (Python):
n = int(input())
overtaking = 0
answer = 0
speed = int(input()[2:])
limits = [300]
for _ in range(n - 1):
    action = input()
    if action[0] == '1':
        speed = int(action[2:])
        while limits[-1] < speed:
            answer += 1
            limits.pop()
    elif action[0] == '2':
        answer += overtaking
        overtaking = 0
    elif action[0] == '3':
        newlimit = int(action[2:])
        if speed > newlimit:
            answer += 1
        else:
            limits.append(newlimit)
    elif action[0] == '4':
        overtaking = 0
    elif action[0] == '5':
        limits = [300]
    elif action[0] == '6':
        overtaking += 1
print(answer)

Solution (Python):
n = int(input())
overtaking = 0
answer = 0
speed = int(input()[2:])
limits = [300]
for _ in range(n - 1):
    action = input()
    if action[0] == '1':
        speed = int(action[2:])
        while limits[-1] < speed:
            answer += 1
            limits.pop()
    elif action[0] == '2':
        answer += overtaking
        overtaking = 0
    elif action[0] == '3':
        newlimit = int(action[2:])
        if speed > newlimit:
            answer += 1
        else:
            limits.append(newlimit)
    elif action[0] == '4':
        overtaking = 0
    elif action[0] == '5':
        limits = [300]
    elif action[0] == '6':
        overtaking += 1
print(answer)

Solution (Python):
n = int(input())
r = 0
cnt = 0
sp = int(input()[2:])
lims = [301]
for ind in range(n - 1):
    s = input()
    if s[0] == '1':
        sp = int(s[2:])
        while lims[-1] < sp:
            cnt += 1
            lims.pop()
    elif s[0] == '2':
        cnt += r
        r = 0
    elif s[0] == '3':
        newlim = int(s[2:])
        if sp > newlim:
            cnt += 1
        else:
            lims.append(newlim)
    elif s[0] == '4':
        r = 0
    elif s[0] == '5':
        lims = [301]
    elif s[0] == '6':
        r += 1
print(cnt)