Ultraviolet A Problem 10191 longest NAP (longest nap time)

Source: Internet
Author: User
Tags dot net

// Longest NAP (longest nap time) // PC/Ultraviolet IDs: 110404/10191, popularity: B, success rate: average level: 1 // verdict: accepted // submission date: 2014-07-28 // UV Run Time: 0.018 S // copyright (c) 2014, Qiu. Metaphysis # Yeah dot net // The previous algorithm has a vulnerability. After being pointed out by a user, the user can use the fill search method to obtain the longest nap time, // to is represented by an array at minute intervals. The time used is 1, and the time not used is 0, set the array element corresponding to the minute between the start time and the end time of the event to 1, and find the maximum length of 0 consecutive times to the maximum hitting/seek time. # Include <iostream> # include <cstdlib> # include <cstring> using namespace STD; # define max_minutes 481int minutes [max_minutes]; // 480 minutes from to struct longestnaptime {int startat; // the start time of the NAP. The duration of the NAP is calculated in minutes starting from, calculated in minutes}; void reset () {memset (minutes, 0, sizeof (minutes); minutes [0] = 0; minutes [480] = 1 ;} int getstarttime (string line) {int starttime = atoi (line. substr (0, 2 ). data () * 6 0; starttime + = atoi (line. substr (3, 2 ). data (); Return (starttime-600);} int getendtime (string line) {int endtime = atoi (line. substr (6, 2 ). data () * 60; endtime + = atoi (line. substr (9, 2 ). data (); Return (endtime-600);} void fill (INT counterevents) {While (counterevents> 0) {string line; Getline (CIN, line ); for (INT I = getstarttime (line); I <getendtime (line); I ++) {minutes [I] = 1;} counterevents --;} Lo Ngestnaptime find () {int startat = 0, naptime = 0; bool starttimesetted = false; longestnaptime thelongest = {startat, 0}; For (INT Index = 0; index <max_minutes; index ++) {If (minutes [Index] = 0) {If (starttimesetted = false) {startat = index; starttimesetted = true;} naptime ++ ;} else {If (naptime> thelongest. duration) {thelongest. startat = startat + 600; thelongest. duration = naptim E;} naptime = 0; starttimesetted = false;} return thelongest;} void print (INT countercases, longestnaptime anaptime) {cout <"day #" <countercases <": the longest nap starts at"; cout <(anaptime. startat/60) <":"; cout <(anaptime. startat % 60 <10? "0": "") <(anaptime. startat % 60); cout <"and will last for"; if (anaptime. duration <60) {cout <anaptime. duration;} else {cout <(anaptime. duration/60) <"hours and" <(anaptime. duration % 60);} cout <"Minutes. \ n ";}int main (int ac, char * AV []) {int countercases = 0, counterevents; while (countercases ++, CIN> counterevents) {cin. ignore (); reset (); fill (counterevents); print (countercases, find ();} return 0 ;}

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.