UVa Problem Solution: 10191 - Longest Nap


Quite simple question. I add two sentinels to avoid boundary checks. However, the input format is tricky to handle. At first, I use scanf("%2d:%2d %2d:%2d%*[^/n]/n, ...) to read the input. After some WAs, I finally realize that there could be nothing left in a line after the time.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10191 C++ "Longest Nap" */
  6. #include <algorithm>
  7. #include <cstdio>
  8. #include <deque>
  9. #include <fstream>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <list>
  13. #include <map>
  14. #include <queue>
  15. #include <set>
  16. #include <stack>
  17. #include <string>
  18. #include <vector>
  19. using namespace std;
  20.      
  21. int const appcount = 102;
  22. int const linesize = 256;
  23.           
  24. struct mytime
  25. {
  26.   mytime(int h = 0, int m = 0) : hour(h), minute(m) {}
  27.   int hour;
  28.   int minute;
  29. };
  30. inline mytime operator-(mytime const& t1, mytime const& t2)
  31. {
  32.   int m = (t1.hour - t2.hour) * 60 + t1.minute - t2.minute;
  33.   return mytime(m / 60, m % 60);
  34. }
  35. inline bool operator>(mytime const& t1, mytime const& t2)
  36. {
  37.   return t1.hour > t2.hour || t1.hour == t2.hour && t1.minute > t2.minute;
  38. }
  39. typedef pair<mytime, mytime> appointment;
  40. struct appcmp
  41. {
  42.   bool operator()(appointment const& a1, appointment const& a2)
  43.   {
  44.     return a1.first.hour < a2.first.hour || 
  45.        a1.first.hour == a2.first.hour && a1.first.minute < a2.first.minute;
  46.   }
  47. };
  48. void find_nap(appointment *apps, int napps, mytime& start, mytime& last)
  49. {
  50.   sort(apps + 1, apps + napps + 1, appcmp());
  51.   last.hour = 0;
  52.   last.minute = 0;
  53.   for (int i = 0; i <= napps; ++i) {
  54.     mytime t = apps[i+1].first - apps[i].second;
  55.     if (t > last) {
  56.       last = t;
  57.       start = apps[i].second;
  58.     }
  59.   }
  60. }
  61. int main(int argc, char *argv[])
  62. {
  63. #ifndef ONLINE_JUDGE
  64.   freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
  65.   freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
  66. #endif
  67.   int napps;
  68.   for (int day = 1; cin >> napps; ++day) {
  69.     appointment apps[appcount];
  70.     for (int i = 1, c; i <= napps; ++i) {
  71.       scanf("%2d:%2d %2d:%2d"
  72.             &apps[i].first.hour, &apps[i].first.minute,
  73.             &apps[i].second.hour, &apps[i].second.minute);
  74.       cin.ignore(2048, '/n');
  75.     }
  76.     apps[0] = make_pair(mytime(9, 0), mytime(10, 0));
  77.     apps[napps+1] = make_pair(mytime(18, 0), mytime(19, 0));
  78.     mytime start, last;
  79.     find_nap(apps, napps, start, last);
  80.     printf("Day #%d: the longest nap starts at %02d:%02d and will last for ",
  81.            day, start.hour, start.minute);
  82.     if (last.hour > 0) printf("%d hours and ", last.hour);
  83.     printf("%d minutes./n", last.minute);
  84.   }
  85.   return 0;
  86. }

發佈了69 篇原創文章 · 獲贊 2 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章