Guest User

Untitled

a guest
Feb 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct event { int change; int time; };
  5. // just compare the times in two given struct event. nothing to see here...
  6. static int comp(const void * a, const void * b)
  7. { if (((struct event*)a)->time > ((struct event*)b)->time) return 1; return -1; }
  8.  
  9. int main(int argc, char * argv[])
  10. {
  11. int rates[3];
  12. fscanf(stdin, "%d %d %d\n", &rates[0], &rates[1], &rates[2]);
  13.  
  14. // set up events change fields to match the order in which we store them when reading input
  15. // (initial order is really completely irrelevant as long as each time has correct change flag)
  16. struct event times[6] = { { 1, 0 }, { -1, 0 }, { 1, 0 }, { -1, 0 }, { 1, 0 }, { -1, 0 }, };
  17. fscanf(stdin, "%d %d\n", &times[0].time, &times[1].time);
  18. fscanf(stdin, "%d %d\n", &times[2].time, &times[3].time);
  19. fscanf(stdin, "%d %d\n", &times[4].time, &times[5].time);
  20.  
  21. qsort(times, 6, sizeof(struct event), comp); // get all events in chronological order
  22.  
  23. int ncars = 0; // note 0 cars means 1. this is adapted for 0-indexed rates table
  24. int sum = 0;
  25. for (int i = 0; i < 5; i++) {
  26. int tdelta = times[i + 1].time - times[i].time;
  27. sum += ncars * rates[ncars] * tdelta;
  28. ncars += times[i + 1].change;
  29. }
  30. printf("%d\n", sum);
  31. }
Add Comment
Please, Sign In to add comment