Advertisement
agul

Airport passengers estimation by flight timetable

Jun 2nd, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <utility>
  5. #include <algorithm>
  6.  
  7. const int OPEN = 0;
  8. const int CLOSE = 1;
  9.  
  10. const int PASSENGERS = 180;
  11. const int INTERVAL_LENGTH = 60;
  12. const double INTENSITY = ((double)PASSENGERS) / INTERVAL_LENGTH;
  13.  
  14. struct Event {
  15.  
  16.     int type, minutes;
  17.  
  18.     Event(int type, int minutes) : type(type), minutes(minutes) {}
  19.  
  20.     bool operator < (const Event& b) {
  21.         return (minutes < b.minutes || minutes == b.minutes && type == CLOSE && b.type == OPEN);
  22.     }
  23.  
  24. };
  25.  
  26. int timeToMinutes(int h, int m) {
  27.     return h * 60 + m;
  28. }
  29.  
  30. void minutesToTime(int minutes, int& h, int& m) {
  31.     h = minutes / 60;
  32.     m = minutes % 60;
  33. }
  34.  
  35. int main() {
  36.     freopen("input.txt", "r", stdin);
  37.     freopen("output.txt", "w", stdout);
  38.     int hours, minutes;
  39.     std::vector<Event> events;
  40.     while (scanf("%d:%d", &hours, &minutes) == 2) {
  41.         int intervalEnd = timeToMinutes(hours, minutes) - 30,
  42.             intervalBegin = intervalEnd - INTERVAL_LENGTH;
  43.         events.push_back(Event(OPEN, intervalBegin));
  44.         events.push_back(Event(CLOSE, intervalEnd));
  45.     }
  46.     std::sort(events.begin(), events.end());
  47.     double currentIntensity = 0;
  48.     std::vector<std::pair<int, double>> changes; // пара: минуты, интенсивность
  49.     for (auto& it : events) {
  50.         if (it.type == OPEN) {
  51.             currentIntensity += INTENSITY;
  52.         } else {
  53.             currentIntensity -= INTENSITY;
  54.         }
  55.         if (changes.empty() || it.minutes > changes.back().first) {
  56.             changes.push_back(std::make_pair(it.minutes, currentIntensity));
  57.         } else {
  58.             changes.back().second = currentIntensity;
  59.         }
  60.     }
  61.     for (auto& it : changes) {
  62.         int hours, minutes;
  63.         double currentIntensity = it.second;
  64.         minutesToTime(it.first, hours, minutes);
  65.         printf("%02d:%02d %.2lf\n", hours, minutes, currentIntensity);
  66.     }
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement