2013年8月16日

UVa 10191 - Longest Nap

#include <cstdio>

const int START = 10 * 60, END = 18 * 60;

int main() {
  char s[999];
  int C = 1;
  while (gets(s)) {
    int n;
    sscanf(s, "%d", &n);
    bool doSomething[END + 2] = {};
    while (n--) {
      gets(s);
      s[5] = s[11] = 0;
      int h1, m1, h2, m2;
      sscanf(s, "%d:%d", &h1, &m1);
      sscanf(s + 6, "%d:%d", &h2, &m2);
      int t1 = h1 * 60 + m1, t2 = h2 * 60 + m2;
      while (t1 < t2) {
        doSomething[t1++] = true;
      }
    }
    doSomething[END] = true;
    int max = 0, len = 0, start;
    for (int i = START; i < END + 2; i++) {
      if (!doSomething[i]) {
        len++;
      } else {
        if (max < len) {
          start = i - len;
          max = len;
        }
        len = 0;
      }
    }
    printf("Day #%d: the longest nap starts at %2d:%02d and will last for ", C++, start / 60, start % 60);
    if (max < 60) {
      printf("%d minutes.\n", max);
    } else {
      printf("%d hours and %d minutes.\n", max / 60, max % 60);
    }
  }
  return 0;
}

沒有留言:

張貼留言