UV problem solution: 10191-longest nap

Source: Internet
Author: User

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 cocould 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. Int const appcount = 102;
  21. Int const linesize = 256;
  22. Struct mytime
  23. {
  24. Mytime (INT h = 0, int m = 0): hour (H), minute (m ){}
  25. Int hour;
  26. Int minute;
  27. };
  28. Inline mytime operator-(mytime const & T1, mytime const & T2)
  29. {
  30. Int M = (t1.hour-t2.hour) * 60 + t1.minute-t2.minute;
  31. Return mytime (M/60, M % 60 );
  32. }
  33. Inline bool operator> (mytime const & T1, mytime const & T2)
  34. {
  35. Return t1.hour> t2.hour | t1.hour = t2.hour & t1.minute> t2.minute;
  36. }
  37. Typedef pair <mytime, mytime> appointment;
  38. Struct appcmp
  39. {
  40. Bool operator () (Appointment const & A1, appointment const & A2)
  41. {
  42. Return a1.first. Hour <a2.first. Hour |
  43. A1.first. Hour = a2.first. Hour & a1.first. Minute <a2.first. minute;
  44. }
  45. };
  46. Void find_nap (Appointment * apps, int napps, mytime & START, mytime & last)
  47. {
  48. Sort (apps + 1, apps + napps + 1, appcmp ());
  49. Last. Hour = 0;
  50. Last. Minute = 0;
  51. For (INT I = 0; I <= napps; ++ I ){
  52. Mytime T = apps [I + 1]. First-apps [I]. Second;
  53. If (T> last ){
  54. Last = T;
  55. Start = apps [I]. Second;
  56. }
  57. }
  58. }
  59. Int main (INT argc, char * argv [])
  60. {
  61. # Ifndef online_judge
  62. Freopen (string (argv [0]) + ". In"). c_str (), "r", stdin );
  63. Freopen (string (argv [0]) + ". Out"). c_str (), "W", stdout );
  64. # Endif
  65. Int napps;
  66. For (INT day = 1; CIN> napps; ++ day ){
  67. Appointment apps [appcount];
  68. For (INT I = 1, C; I <= napps; ++ I ){
  69. Scanf ("% 2D: % 2D % 2D: % 2D ",
  70. & Apps [I]. First. Hour, & apps [I]. First. Minute,
  71. & Apps [I]. Second. Hour, & apps [I]. Second. Minute );
  72. Cin. Ignore (2048, '/N ');
  73. }
  74. Apps [0] = make_pair (mytime (9, 0), mytime (10, 0 ));
  75. Apps [napps + 1] = make_pair (mytime (18, 0), mytime (19, 0 ));
  76. Mytime start, last;
  77. Find_nap (apps, napps, start, last );
  78. Printf ("day # % d: The longest nap starts at % 02d: % 02d and will last ",
  79. Day, start. Hour, start. Minute );
  80. If (last. Hour> 0) printf ("% d hours and", last. hour );
  81. Printf ("% d minutes./N", last. Minute );
  82. }
  83. Return 0;
  84. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.