Bus Assignment

Question

Solution

Idea

As the first ICPC question I tried, this question is not that hard. Basically, the idea is:

  1. use two variables cur (the current number of passengers on the bus) and max (the maximum number of passengers on the bus).

  2. Every time you arrived at a bus stop, update the cur based on the number of passengers get on and off. After that, compare cur with max, if cur is bigger, then max=cur.

Code

https://github.com/mendax1234/Coding-Problems/blob/main/kattis/busassignment/busassignment.c
#include <stdio.h>

int main()
{
  int n;
  scanf("%d\n", &n);
  long cur = 0;
  long max = 0;
  for (int i = 0; i < n; i += 1)
  {
    int get_off, get_on;
    scanf("%d %d\n", &get_off, &get_on);
    cur -= get_off;
    cur += get_on;
    if (cur >= max)
    {
      max = cur;
    }
  }
  printf("%ld\n", max);
}

Last updated