Problem p01621 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>

using namespace std;

int counter(int s, int t, string weekday, string time, int m, int begin) {
  map<string, int> week;
  map<string, int>::iterator ite;
  // week["All"] = 0;
  week["Sun"] = 0;
  week["Mon"] = 1440;
  week["Tue"] = 2880;
  week["Wed"] = 4320;
  week["Thu"] = 5760;
  week["Fri"] = 7200;
  week["Sat"] = 8640;

  int count = 0;
  int start = begin;
  int mid;
  int end;

  for (int i = start, j = 0; j < m; i += t, j++) {
    i = i % 10080;
    mid = (i + (s / 2)) % 10080;
    end = (i + s) % 10080;
    if (weekday == "All") {
      if (time == "Day") {
        for (ite = week.begin(); ite != week.end(); ++ite) {
          if ((((week[ite->first] + (60 * 6)) <= i) &&
               ((week[ite->first] + (60 * 18)) > i)) &&
              (((week[ite->first] + (60 * 6)) <= mid) &&
               ((week[ite->first] + (60 * 18)) > mid)) &&
              (((week[ite->first] + (60 * 6)) <= end) &&
               ((week[ite->first] + (60 * 18)) > end))) {
            count++;
            break;
          }
        }
      } else if (time == "Night") {
        for (ite = week.begin(); ite != week.end(); ++ite) {
          if (ite->first == "Sun") {
            if ((((week[ite->first] <= i) &&
                  ((week[ite->first] + (60 * 6)) > i)) ||
                 ((week["Sat"] + (60 * 18) <= i) &&
                  ((week["Sat"] + (60 * 24)) > i))) &&
                (((week[ite->first] <= mid) &&
                  ((week[ite->first] + (60 * 6)) > mid)) ||
                 ((week["Sat"] + (60 * 18) <= mid) &&
                  ((week["Sat"] + (60 * 24)) > mid))) &&
                (((week[ite->first] <= end) &&
                  ((week[ite->first] + (60 * 6)) > end)) ||
                 ((week["Sat"] + (60 * 18) <= end) &&
                  ((week["Sat"] + (60 * 24)) > end)))) {
              count++;
              break;
            } else if ((((week[ite->first] + (60 * 18)) <= i) &&
                        ((week[ite->first] + (60 * 30)) > i)) &&
                       (((week[ite->first] + (60 * 18)) <= mid) &&
                        ((week[ite->first] + (60 * 30)) > mid)) &&
                       (((week[ite->first] + (60 * 18)) <= end) &&
                        ((week[ite->first] + (60 * 30)) > end))) {
              count++;
              break;
            }
          }
          if (ite->first == "Sat") {
            if (((week[ite->first] - (60 * 6) <= i) &&
                 ((week[ite->first] + (60 * 6)) > i)) &&
                ((week[ite->first] - (60 * 6) <= mid) &&
                 ((week[ite->first] + (60 * 6)) > mid)) &&
                ((week[ite->first] - (60 * 6) <= end) &&
                 ((week[ite->first] + (60 * 6)) > end))) {
              count++;
              break;
            } else if (((((week[ite->first] + (60 * 18)) <= i) &&
                         ((week[ite->first] + (60 * 24)) > i)) ||
                        ((week["Sun"] <= i) &&
                         ((week["Sun"] + (60 * 6)) > i))) &&
                       ((((week[ite->first] + (60 * 18)) <= mid) &&
                         ((week[ite->first] + (60 * 24)) > mid)) ||
                        ((week["Sun"] <= mid) &&
                         ((week["Sun"] + (60 * 6)) > mid))) &&
                       ((((week[ite->first] + (60 * 18)) <= end) &&
                         ((week[ite->first] + (60 * 24)) > end)) ||
                        ((week["Sun"] <= end) &&
                         ((week["Sun"] + (60 * 6)) > end)))) {
              count++;
              break;
            }
          } else {
            if (((week[ite->first] - (60 * 6) <= i) &&
                 ((week[ite->first] + (60 * 6)) > i)) &&
                ((week[ite->first] - (60 * 6) <= mid) &&
                 ((week[ite->first] + (60 * 6)) > mid)) &&
                ((week[ite->first] - (60 * 6) <= end) &&
                 ((week[ite->first] + (60 * 6)) > end))) {
              count++;
              break;
            } else if ((((week[ite->first] + (60 * 18)) <= i) &&
                        ((week[ite->first] + (60 * 30)) > i)) &&
                       (((week[ite->first] + (60 * 18)) <= mid) &&
                        ((week[ite->first] + (60 * 30)) > mid)) &&
                       (((week[ite->first] + (60 * 18)) <= end) &&
                        ((week[ite->first] + (60 * 30)) > end))) {
              count++;
              break;
            }
          }
        }
      } else {
        count++;
      }
    } else {
      if (time == "Day") {
        if ((((week[weekday] + (60 * 6)) <= i) &&
             ((week[weekday] + (60 * 18)) > i)) &&
            (((week[weekday] + (60 * 6)) <= mid) &&
             ((week[weekday] + (60 * 18)) > mid)) &&
            (((week[weekday] + (60 * 6)) <= end) &&
             ((week[weekday] + (60 * 18)) > end))) {
          count++;
        }
      } else if (time == "Night") {
        if (((week[weekday] <= i) && ((week[weekday] + (60 * 6)) > i)) &&
            ((week[weekday] <= mid) && ((week[weekday] + (60 * 6)) > mid)) &&
            ((week[weekday] <= end) && ((week[weekday] + (60 * 6)) > end))) {
          count++;
        } else if ((((week[weekday] + (60 * 18)) <= i) &&
                    ((week[weekday] + (60 * 24)) > i)) &&
                   (((week[weekday] + (60 * 18)) <= mid) &&
                    ((week[weekday] + (60 * 24)) > mid)) &&
                   (((week[weekday] + (60 * 18)) <= end) &&
                    ((week[weekday] + (60 * 24)) > end))) {
          count++;
        }
      } else {
        if (((week[weekday] <= i) && ((week[weekday] + (60 * 24)) > i)) &&
            ((week[weekday] <= mid) && ((week[weekday] + (60 * 24)) > mid)) &&
            ((week[weekday] <= end) && ((week[weekday] + (60 * 24)) > end))) {
          count++;
        }
      }
    }
  }

  return count;
}

int main() {
  int s, n, t, p, m;
  string weekday, time;

  while (cin >> s >> n >> t >> weekday >> time >> p >> m) {
    if ((s == 0) && (n == 0) && (t == 0) && (weekday == "None") &&
        (time == "None") && (p == 0) && (m == 0)) {
      return 0;
    }
    int count;
    int max = 0;

    for (int i = 0; i < 10080; i++) {
      count = counter(s, t, weekday, time, m, i);
      if (max < count) {
        max = count;
      }
    }

    printf("%1.10f\\n", 1.0 - pow((double)(p - 1) / p, n * max));
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>

using namespace std;

// #define come(i,j) cout << i << ":" << j << endl

int counter(int s, int t, string weekday, string time, int m, int begin) {
  map<string, int> week;
  map<string, int>::iterator ite;
  week["All"] = 0;
  week["Sun"] = 0;
  week["Mon"] = 1440;
  week["Tue"] = 2880;
  week["Wed"] = 4320;
  week["Thu"] = 5760;
  week["Fri"] = 7200;
  week["Sat"] = 8640;

  int count = 0;
  int start = begin;

  if (time == "Day") {
    start += (60 * 6);
  } else if ((time == "Night") && (weekday == "All")) {
    start += (60 * 18);
  } else if ((time == "Night") || (time == "All")) {
    ;
  } else {
    cout << "exit_t" << endl;
    exit(0);
  }

  for (int i = start, j = 0; j < (t * m); i++, j++) {
    i = i % 10080;
    if (weekday == "All") {
      if (time == "Day") {
        for (ite = week.begin(); ite != week.end(); ++ite) {
          if (((week[ite->first] + (60 * 6)) <= i) &&
              ((week[ite->first] + (60 * 18)) > i) && ((j % t) == s)) {
            count++; /*come(i,j);*/
            break;
          }
        }
      } else if (time == "Night") {
        for (ite = week.begin(); ite != week.end(); ++ite) {
          if (((week[ite->first] <= i) && ((week[ite->first] + (60 * 6)) > i) &&
               ((j % t) == s)) ||
              (((week[ite->first] + (60 * 18)) <= i) &&
               ((week[ite->first] + (60 * 24)) > i) && ((j % t) == s))) {
            count++; /*come(i,j);*/
            break;
          }
        }
      } else {
        if ((j % t) == s) {
          count++; /*come(i,j);*/
        }
      }
    } else {
      if (time == "Day") {
        if (((week[weekday] + (60 * 6)) <= i) &&
            ((week[weekday] + (60 * 18)) > i) && ((j % t) == s)) {
          count++; /*come(i,j);*/
        }
      } else if (time == "Night") {
        if (((week[weekday] <= i) && ((week[weekday] + (60 * 6)) > i) &&
             ((j % t) == s)) ||
            (((week[weekday] + (60 * 18)) <= i) &&
             ((week[weekday] + (60 * 24)) > i) && ((j % t) == s))) {
          count++; /*come(i,j);*/
        }
      } else {
        if ((week[weekday] <= i) && ((week[weekday] + (60 * 24)) > i) &&
            ((j % t) == s)) {
          count++; /*come(i,j);*/
        }
      }
    }
  }

  return count;
}

int main() {
  int s, n, t, p, m;
  string weekday, time;

  while (cin >> s >> n >> t >> weekday >> time >> p >> m) {
    if ((s == 0) && (n == 0) && (t == 0) && (weekday == "None") &&
        (time == "None") && (p == 0) && (m == 0)) {
      return 0;
    }
    int count = 0;
    int max = 0;
    for (int i = 0; i < 10080; i++) {
      count = counter(s, t, weekday, time, m, i);
      if (max < count) {
        max = count;
      }
    }
    printf("%1.10f\\n", 1.0 - pow((double)(p - 1) / p, n * max));
  }
}