Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct event { int change; int time; };
- // just compare the times in two given struct event. nothing to see here...
- static int comp(const void * a, const void * b)
- { if (((struct event*)a)->time > ((struct event*)b)->time) return 1; return -1; }
- int main(int argc, char * argv[])
- {
- int rates[3];
- fscanf(stdin, "%d %d %d\n", &rates[0], &rates[1], &rates[2]);
- // set up events change fields to match the order in which we store them when reading input
- // (initial order is really completely irrelevant as long as each time has correct change flag)
- struct event times[6] = { { 1, 0 }, { -1, 0 }, { 1, 0 }, { -1, 0 }, { 1, 0 }, { -1, 0 }, };
- fscanf(stdin, "%d %d\n", ×[0].time, ×[1].time);
- fscanf(stdin, "%d %d\n", ×[2].time, ×[3].time);
- fscanf(stdin, "%d %d\n", ×[4].time, ×[5].time);
- qsort(times, 6, sizeof(struct event), comp); // get all events in chronological order
- int ncars = 0; // note 0 cars means 1. this is adapted for 0-indexed rates table
- int sum = 0;
- for (int i = 0; i < 5; i++) {
- int tdelta = times[i + 1].time - times[i].time;
- sum += ncars * rates[ncars] * tdelta;
- ncars += times[i + 1].change;
- }
- printf("%d\n", sum);
- }
Add Comment
Please, Sign In to add comment