Travel Card (Codeforces 760D)
Tags: DP, two pointers

A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare.The fare is constructed in the following manner. There are three types of tickets:   a ticket for one trip costs 20 byteland rubles,  a ticket for 90 minutes costs 50 byteland rubles,  a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute.To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b.You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.

Input: The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger.Each of the following n lines contains the time of trip ti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e. ti + 1 > ti holds for all 1 ≤ i < n.

Output: Output n integers. For each trip, print the sum the passenger is charged after it.

Note: In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.

Sample Inputs:
3
10
20
30

10
13
45
46
60
103
115
126
150
256
516

Sample Outputs:
20
20
10

20
20
10
0
20
0
0
20
20
10

Solution (Python):
n = int(input())
times = []
first_90 = 0
first_1440 = 0
sum_90 = 0
sum_1440 = 0
values = []
for i in range(n):
    times.append(int(input()))
    while times[first_90] <= times[-1] - 90:
        sum_90 -= values[first_90]
        first_90 += 1
    while times[first_1440] <= times[-1] - 1440:
        sum_1440 -= values[first_1440]
        first_1440 += 1
    values.append(min([20, 50 - sum_90, 120 - sum_1440]))
    sum_90 += values[-1]
    sum_1440 += values[-1]
print(*values, sep='\n')

Solution (Python):
n = int(input())
a = [0]
for i in range(n):
    x = int(input())
    a.append(x)
dp = [0] * (n + 1)
dp[0] = 0
p90 = 1
p1440 = 1
for i in range(1, n + 1):
    dp[i] = dp[i - 1] + 20
    while a[p90] + 90 <= a[i]:
        p90 = p90 + 1
    dp[i] = min(dp[i], dp[p90 - 1] + 50)
    while a[p1440] + 1440 <= a[i]:
        p1440 = p1440 + 1
    dp[i] = min(dp[i], dp[p1440 - 1] + 120)
for i in range(1, n + 1):
    print(dp[i] - dp[i - 1])

Solution (Python):
n = int(input())
time = []
for i in range(n):
    time.append(int(input()))
ptr90 = 0
ptrs = 0
dp = [0] * n
dp[0] = 20
print(20)
for i in range(1, n):
    s1 = dp[i - 1] + 20
    if time[i] <= 89 + time[ptr90]:
        if ptr90 == 0:
            s2 = 50
        else:
            s2 = dp[ptr90 - 1] + 50
    else:
        while time[ptr90] + 89 < time[i]:
            ptr90 += 1
        s2 = dp[ptr90 - 1] + 50
    if time[i] <= 1439 + time[ptrs]:
        if ptrs == 0:
            s3 = 120
        else:
            s3 = dp[ptrs - 1] + 120
    else:
        while time[ptrs] + 1439 < time[i]:
            ptrs += 1
            s3 = dp[ptrs - 1] + 120
    dp[i] = min(s1, s2, s3)
    print(dp[i] - dp[i - 1])