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:
- /*************************************** **********************************
- * Copyright (c) 2008 by liukaipeng *
- * Liukaipeng at gmail dot com *
- **************************************** *********************************/
- /* @ Judge_id 00000 10191 C ++ "Longest nap "*/
- # Include <algorithm>
- # Include <cstdio>
- # Include <deque>
- # Include <fstream>
- # Include <iomanip>
- # Include <iostream>
- # Include <list>
- # Include <map>
- # Include <queue>
- # Include <set>
- # Include <stack>
- # Include <string>
- # Include <vector>
- Using namespace STD;
- Int const appcount = 102;
- Int const linesize = 256;
- Struct mytime
- {
- Mytime (INT h = 0, int m = 0): hour (H), minute (m ){}
- Int hour;
- Int minute;
- };
- Inline mytime operator-(mytime const & T1, mytime const & T2)
- {
- Int M = (t1.hour-t2.hour) * 60 + t1.minute-t2.minute;
- Return mytime (M/60, M % 60 );
- }
- Inline bool operator> (mytime const & T1, mytime const & T2)
- {
- Return t1.hour> t2.hour | t1.hour = t2.hour & t1.minute> t2.minute;
- }
- Typedef pair <mytime, mytime> appointment;
- Struct appcmp
- {
- Bool operator () (Appointment const & A1, appointment const & A2)
- {
- Return a1.first. Hour <a2.first. Hour |
- A1.first. Hour = a2.first. Hour & a1.first. Minute <a2.first. minute;
- }
- };
- Void find_nap (Appointment * apps, int napps, mytime & START, mytime & last)
- {
- Sort (apps + 1, apps + napps + 1, appcmp ());
- Last. Hour = 0;
- Last. Minute = 0;
- For (INT I = 0; I <= napps; ++ I ){
- Mytime T = apps [I + 1]. First-apps [I]. Second;
- If (T> last ){
- Last = T;
- Start = apps [I]. Second;
- }
- }
- }
- Int main (INT argc, char * argv [])
- {
- # Ifndef online_judge
- Freopen (string (argv [0]) + ". In"). c_str (), "r", stdin );
- Freopen (string (argv [0]) + ". Out"). c_str (), "W", stdout );
- # Endif
- Int napps;
- For (INT day = 1; CIN> napps; ++ day ){
- Appointment apps [appcount];
- For (INT I = 1, C; I <= napps; ++ I ){
- Scanf ("% 2D: % 2D % 2D: % 2D ",
- & Apps [I]. First. Hour, & apps [I]. First. Minute,
- & Apps [I]. Second. Hour, & apps [I]. Second. Minute );
- Cin. Ignore (2048, '/N ');
- }
- Apps [0] = make_pair (mytime (9, 0), mytime (10, 0 ));
- Apps [napps + 1] = make_pair (mytime (18, 0), mytime (19, 0 ));
- Mytime start, last;
- Find_nap (apps, napps, start, last );
- Printf ("day # % d: The longest nap starts at % 02d: % 02d and will last ",
- Day, start. Hour, start. Minute );
- If (last. Hour> 0) printf ("% d hours and", last. hour );
- Printf ("% d minutes./N", last. Minute );
- }
- Return 0;
- }