Problem
There are many vehicles between the rivers and one ferry is alternately transporting vehicles from the left, find the minimum number of deliveries.
Algorithm
Greedy. Just load the most cars on one side at a each time and simulate the process.
Code
#include <stdio.h>
#include <stdlib.h>
char name[10];
int car[2][10001];
int main()
{
int c, l, m, car_length;
scanf("%d", &c);
while (c --) {
int count[2] = {0, 0};
scanf("%d %d", &l, &m);
for (int i = 0; i < m; ++i) {
scanf("%d %s", &car_length, name);
if (name[0] == 'l') {
car[0][count[0] ++] = car_length;
}else if (name[0] == 'r') {
car[1][count[1] ++] = car_length;
}
}
int pointer[2] = {0, 0}, ans = 0, on_ship = 0, id = 0;
while (pointer[0] < count[0] || pointer[1] < count[1]) {
while (on_ship + car[id][pointer[id]] < l * 100 && pointer[id] < count[id])
on_ship += car[id][pointer[id] ++];
// printf("state %d, li %d/%d , ri %d/%d\n", id, pointer[0], count[0], pointer[1], count[1]);
ans ++;
on_ship = 0;
id = (id+1)%2;
}
printf("%d\n", ans);
}
return 0;
}