// Longest NAP (longest nap time) // PC/Ultraviolet IDs: 110404/10191, popularity: B, success rate: average level: 1 // verdict: accepted // submission date: 2011-05-21 // ultraviolet A run time: 0.008 s /// copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // converts the time to an integer for sorting, and then finds the maximum idle time between the two. # Include <iostream> # include <algorithm> # include <cstdlib> using namespace STD; # define maxsize (100 + 2) // number of events + two pre-added events. Struct time_format {string time; // the end time of the event. String representation. Int start; // event start time, which is the number of hours * 60 + minutes. Int end; // event end time, which is the number of hours * 60 + minutes .}; Bool CMP (time_format X, time_format y) {If (X. Start! = Y. start) return X. start <Y. start; return X. end <Y. end;} void reset (time_format T []) {// you need to add the time pairs and to the array in advance. Because the event may not start at //, there is a period of free time before and the start time, similarly, there may be idle time between events before and. T [0]. time = "10: 00"; t [0]. start = 600; t [0]. end = 600; t [1]. time = "18:00"; t [1]. start = 1080; t [1]. end = 1080;} int main (int ac, char * AV []) {time_format T [maxsize]; INT cases = 0; int events; string line; while (cases ++, CIN> events) {cin. ignore (); int counter = 2; reset (t); While (counter-2) <events) {Getline (CIN, line); t [Counter]. start = atoi (line. substr (0, 2 ). data () * 60; t [Counter]. start + = atoi (line. s Ubstr (3, 2 ). data (); t [Counter]. end = atoi (line. substr (6, 2 ). data () * 60; t [Counter]. end + = atoi (line. substr (9, 2 ). data (); t [Counter]. time = line. substr (6, 5); counter ++;} // sort. Sort (t, t + events + 2, CMP); // find the gap with the maximum time span. Int longest_nap = 0; string longest_start; For (INT I = 0; I <events + 1; I ++) if (T [I + 1]. start-T [I]. end)> longest_nap) {longest_start = T [I]. time; longest_nap = T [I + 1]. start-T [I]. end;} // output the start time and duration of the maximum nap time. Cout <"day #" <cases <": the longest nap starts at"; cout <longest_start <"and will last for"; if (longest_nap <60) cout <longest_nap; elsecout <(longest_nap/60) <"hours and" <(longest_nap % 60); cout <"Minutes. \ n ";}return 0 ;}