Bus Assignment
Question
Solution
Idea
As the first ICPC question I tried, this question is not that hard. Basically, the idea is:
use two variables
cur
(the current number of passengers on the bus) andmax
(the maximum number of passengers on the bus).Every time you arrived at a bus stop, update the
cur
based on the number of passengers get on and off. After that, comparecur
withmax
, ifcur
is bigger, thenmax=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