Official

B - Ticket Counter Editorial by en_translator


Suppose that the \(i\)-th person finishes buying a ticket at time \(x_i\). For convenience, let \(x_0=0\).

For each \(i\ (1\leq i\leq N)\), let us determine how to represent the value \(x_i\) using the value \(x_{i-1}\).

  • If \(x_{i-1} \leq T_i\): there is no queue when the \(i\)-th person visits the ticket booth, so they can start purchasing instantly. Thus, \(x_i=T_i+A\).
  • If \(x_{i-1} > T_i\): there is a person who is still purchasing a ticket when the \(i\)-th person visits the booth, so they starts the purchasing process right after the \((i-1)\)-th person finishes. Thus, \(x_i=x_{i-1}+A\).

To roll up,\(x_i=\max(x_{i-1},T_i)+A\). One can use this expression to find \(x_i\) for each \(i=1,2,\dots,N\) in this order.

Sample code (C++):

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, a;
    cin >> n >> a;
    int pre = 0;
    for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        int ans = max(pre, t) + a;
        cout << ans << '\n';
        pre = ans;
    }
}

Sample code (Python):

n, a = map(int, input().split())
t = list(map(int, input().split()))
pre = 0
for i in range(n):
    ans = max(pre, t[i]) + a
    print(ans)
    pre = ans

posted:
last update: