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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pqgreater(x) x, vector<x>, greater<x>
#define decimal(x) cout << fixed << setprecision(x)
#define dbg(x) cerr << #x << ": " << x << endl;
#define gc getchar         //_unlocked
#define pc putchar         //_unlocked
const int mod = 998244353; // 1e9+7 or 998244353, remove const if changing mods
const ld eps = 1e-6;

ld sign(ld x) { return (x > 0) - (x < 0); }
ll sign(ll x) { return (x > 0) - (x < 0); }
int sign(int x) { return (x > 0) - (x < 0); }
struct pt {
  ld x, y;
  pt() {}
  pt(ld _x, ld _y) : x(_x), y(_y) {}
  bool operator<(pt p) { return tie(x, y) < tie(p.x, p.y); }
  bool operator==(pt p) { return tie(x, y) == tie(p.x, p.y); }
  pt operator+(pt p) { return {x + p.x, y + p.y}; }
  pt operator-(pt p) { return {x - p.x, y - p.y}; }
  pt operator*(ld d) { return {x * d, y * d}; }
  pt operator/(ld d) { return {x / d, y / d}; }
  ld dot(pt p) { return x * p.x + y * p.y; }
  ld det(pt p) { return x * p.y - y * p.x; }
  ld cross(pt p, pt q) { return (p - *this).det(q - *this); }
  ld dist() { return sqrtl(x * x + y * y); }
  pt rot90() { return pt(-y, x); }
  pt unit() { return *this / dist(); }
  ld angle() { return atan2(y, x); }
  pt rot(ld an) {
    return {x * cos(an) - y * sin(an), x * sin(an) + y * cos(an)};
  }
  void read() { cin >> x >> y; }
  void write() { cout << "(" << x << "," << y << ")" << endl; }
};

struct seg {
  pt fi, sc;
  seg() {}
  seg(pt _fi, pt _sc) : fi(_fi), sc(_sc) {}
  ld dist(pt C) {
    if (fi == sc)
      return (C - fi).dist();
    ld d = pow((sc - fi).dist(), 2);
    ld t = min(d, max((ld)0.0, (C - fi).dot(sc - fi)));
    return ((C - fi) * d - (sc - fi) * t).dist() / d;
  }
  ld len() { return (fi - sc).dist(); }
  bool on_seg(pt C) {
    return abs(C.cross(fi, sc)) < eps && (fi - C).dot(sc - C) <= eps;
  }
  vector<pt> intersect(seg rhs) {
    ld oa = rhs.fi.cross(rhs.sc, fi);
    ld ob = rhs.fi.cross(rhs.sc, sc);
    ld oc = fi.cross(sc, rhs.fi);
    ld od = fi.cross(sc, rhs.sc);
    if (sign(oa) * sign(ob) < 0 && sign(oc) * sign(od) < 0)
      return {(fi * ob - sc * oa) / (ob - oa)};
    vector<pt> ans;
    if (rhs.on_seg(fi))
      ans.push_back(fi);
    if (rhs.on_seg(sc))
      ans.push_back(sc);
    if (on_seg(rhs.fi))
      ans.push_back(rhs.fi);
    if (on_seg(rhs.sc))
      ans.push_back(rhs.sc);
    return ans;
  }
};

struct line {
  ld a, b, c; // line a*x+b*y=c
  line(pt p1, pt p2) {
    assert(!(p1 == p2));
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = a * p1.x + b * p1.y;
  }
  line() {}
  line(ld _a, ld _b, ld _c) : a(_a), b(_b), c(_c) {}
  ld dist(pt p) { return fabs(a * p.x + b * p.y - c) / sqrtl(a * a + b * b); }
  pair<int, pt> intersect(line rhs) {
    ld dett = a * rhs.b - b * rhs.a;
    if (fabs(dett) <= eps) {
      ld det2 = c * rhs.a - a * rhs.c;
      if (fabs(det2) <= eps)
        return {-1, pt()}; // infinte
      return {0, pt()};    // no sol
    }
    return {1,
            pt((c * rhs.b - rhs.c * b) / dett, (a * rhs.c - c * rhs.a) / dett)};
  }
};

pt reflect(pt A, line L) {
  line perpendicular(-L.b, L.a, -L.b * A.x + L.a * A.y);
  pt insect = perpendicular.intersect(L).second;
  return insect * (ld)2.0 - A;
}

struct mint {
  int val;
  mint() { val = 0; }
  mint(const ll &v) {
    val = (-mod <= v && v < mod) ? v : v % mod;
    if (val < 0)
      val += mod;
  }
  friend ostream &operator<<(ostream &os, const mint &a) { return os << a.val; }
  friend bool operator==(const mint &a, const mint &b) {
    return a.val == b.val;
  }
  friend bool operator!=(const mint &a, const mint &b) {
    return a.val != b.val;
  }
  friend bool operator<(const mint &a, const mint &b) { return a.val < b.val; }
  friend bool operator>(const mint &a, const mint &b) { return a.val > b.val; }
  friend bool operator<=(const mint &a, const mint &b) {
    return a.val <= b.val;
  }
  friend bool operator>=(const mint &a, const mint &b) {
    return a.val >= b.val;
  }

  mint operator-() { return mint(-val); }
  mint &operator+=(const mint &m) {
    if ((val += m.val) >= mod)
      val -= mod;
    return *this;
  }
  mint &operator-=(const mint &m) {
    if ((val -= m.val) < mod)
      val += mod;
    return *this;
  }
  mint &operator*=(const mint &m) {
    val = (val * 1ll * m.val) % mod;
    return *this;
  }
  friend mint pow(mint a, ll p) {
    mint ans = 1;
    for (; p; p /= 2, a *= a)
      if (p % 2 == 1)
        ans *= a;
    return ans;
  }
  friend mint inv(const mint &a) {
    assert(a.val);
    return pow(a, mod - 2);
  }
  mint &operator/=(const mint &m) { return (*this) *= inv(m); }
  friend mint operator+(mint a, const mint &b) { return a += b; }
  friend mint operator-(mint a, const mint &b) { return a -= b; }
  friend mint operator*(mint a, const mint &b) { return a *= b; }
  friend mint operator/(mint a, const mint &b) { return a /= b; }
};

/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

int solve();
void precomp();
#define multitest 0
#define usecase 0
#define usedecimal 0
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  if (multitest)
    cin >> t;
  if (usedecimal)
    decimal(10);
  precomp();
  for (int tc = 1; tc <= t; tc++) {
    if (multitest && usecase)
      cout << "Case #" << tc << ": ";
    solve();
  }
}

void precomp() { return; }

int solve() {
  int h1, m1, h2, m2, k;
  cin >> h1 >> m1 >> h2 >> m2 >> k;
  int time;
  if (h1 == h2 && m1 <= m2)
    time = m2 - m1;
  else {
    time = 60 - m1;
    h1++;
    if (h1 > h2)
      h2 += 24;
    time += (h2 - h1) * 60 + m2;
  }
  cout << time - k << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pqgreater(x) x, vector<x>, greater<x>
#define decimal(x) cout << fixed << setprecision(x)
#define dbg(x) cerr << #x << ": " << x << endl;
#define gc getchar         //_unlocked
#define pc putchar         //_unlocked
const int mod = 998244353; // 1e9+7 or 998244353, remove const if changing mods
const ld eps = 1e-6;

ld sign(ld x) { return (x > 0) - (x < 0); }
ll sign(ll x) { return (x > 0) - (x < 0); }
int sign(int x) { return (x > 0) - (x < 0); }
struct pt {
  ld x, y;
  pt() {}
  pt(ld _x, ld _y) : x(_x), y(_y) {}
  bool operator<(pt p) { return tie(x, y) < tie(p.x, p.y); }
  bool operator==(pt p) { return tie(x, y) == tie(p.x, p.y); }
  pt operator+(pt p) { return {x + p.x, y + p.y}; }
  pt operator-(pt p) { return {x - p.x, y - p.y}; }
  pt operator*(ld d) { return {x * d, y * d}; }
  pt operator/(ld d) { return {x / d, y / d}; }
  ld dot(pt p) { return x * p.x + y * p.y; }
  ld det(pt p) { return x * p.y - y * p.x; }
  ld cross(pt p, pt q) { return (p - *this).det(q - *this); }
  ld dist() { return sqrtl(x * x + y * y); }
  pt rot90() { return pt(-y, x); }
  pt unit() { return *this / dist(); }
  ld angle() { return atan2(y, x); }
  pt rot(ld an) {
    return {x * cos(an) - y * sin(an), x * sin(an) + y * cos(an)};
  }
  void read() { cin >> x >> y; }
  void write() { cout << "(" << x << "," << y << ")" << endl; }
};

struct seg {
  pt fi, sc;
  seg() {}
  seg(pt _fi, pt _sc) : fi(_fi), sc(_sc) {}
  ld dist(pt C) {
    if (fi == sc)
      return (C - fi).dist();
    ld d = pow((sc - fi).dist(), 2);
    ld t = min(d, max((ld)0.0, (C - fi).dot(sc - fi)));
    return ((C - fi) * d - (sc - fi) * t).dist() / d;
  }
  ld len() { return (fi - sc).dist(); }
  bool on_seg(pt C) {
    return abs(C.cross(fi, sc)) < eps && (fi - C).dot(sc - C) <= eps;
  }
  vector<pt> intersect(seg rhs) {
    ld oa = rhs.fi.cross(rhs.sc, fi);
    ld ob = rhs.fi.cross(rhs.sc, sc);
    ld oc = fi.cross(sc, rhs.fi);
    ld od = fi.cross(sc, rhs.sc);
    if (sign(oa) * sign(ob) < 0 && sign(oc) * sign(od) < 0)
      return {(fi * ob - sc * oa) / (ob - oa)};
    vector<pt> ans;
    if (rhs.on_seg(fi))
      ans.push_back(fi);
    if (rhs.on_seg(sc))
      ans.push_back(sc);
    if (on_seg(rhs.fi))
      ans.push_back(rhs.fi);
    if (on_seg(rhs.sc))
      ans.push_back(rhs.sc);
    return ans;
  }
};

struct line {
  ld a, b, c; // line a*x+b*y=c
  line(pt p1, pt p2) {
    assert(!(p1 == p2));
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = a * p1.x + b * p1.y;
  }
  line() {}
  line(ld _a, ld _b, ld _c) : a(_a), b(_b), c(_c) {}
  ld dist(pt p) { return fabs(a * p.x + b * p.y - c) / sqrtl(a * a + b * b); }
  pair<int, pt> intersect(line rhs) {
    ld dett = a * rhs.b - b * rhs.a;
    if (fabs(dett) <= eps) {
      ld det2 = c * rhs.a - a * rhs.c;
      if (fabs(det2) <= eps)
        return {-1, pt()}; // infinte
      return {0, pt()};    // no sol
    }
    return {1,
            pt((c * rhs.b - rhs.c * b) / dett, (a * rhs.c - c * rhs.a) / dett)};
  }
};

pt reflect(pt A, line L) {
  line perpendicular(-L.b, L.a, -L.b * A.x + L.a * A.y);
  pt insect = perpendicular.intersect(L).second;
  return insect * (ld)2.0 - A;
}

struct mint {
  int val;
  mint() { val = 0; }
  mint(const ll &v) {
    val = (-mod <= v && v < mod) ? v : v % mod;
    if (val < 0)
      val += mod;
  }
  friend ostream &operator<<(ostream &os, const mint &a) { return os << a.val; }
  friend bool operator==(const mint &a, const mint &b) {
    return a.val == b.val;
  }
  friend bool operator!=(const mint &a, const mint &b) {
    return a.val != b.val;
  }
  friend bool operator<(const mint &a, const mint &b) { return a.val < b.val; }
  friend bool operator>(const mint &a, const mint &b) { return a.val > b.val; }
  friend bool operator<=(const mint &a, const mint &b) {
    return a.val <= b.val;
  }
  friend bool operator>=(const mint &a, const mint &b) {
    return a.val >= b.val;
  }

  mint operator-() { return mint(-val); }
  mint &operator+=(const mint &m) {
    if ((val += m.val) >= mod)
      val -= mod;
    return *this;
  }
  mint &operator-=(const mint &m) {
    if ((val -= m.val) < mod)
      val += mod;
    return *this;
  }
  mint &operator*=(const mint &m) {
    val = (val * 1ll * m.val) % mod;
    return *this;
  }
  friend mint pow(mint a, ll p) {
    mint ans = 1;
    for (; p; p /= 2, a *= a)
      if (p % 2 == 1)
        ans *= a;
    return ans;
  }
  friend mint inv(const mint &a) {
    assert(a.val);
    return pow(a, mod - 2);
  }
  mint &operator/=(const mint &m) { return (*this) *= inv(m); }
  friend mint operator+(mint a, const mint &b) { return a += b; }
  friend mint operator-(mint a, const mint &b) { return a -= b; }
  friend mint operator*(mint a, const mint &b) { return a *= b; }
  friend mint operator/(mint a, const mint &b) { return a /= b; }
};

/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

int solve();
void precomp();
#define multitest 0
#define usecase 0
#define usedecimal 0
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  if (multitest)
    cin >> t;
  if (usedecimal)
    decimal(10);
  precomp();
  for (int tc = 1; tc <= t; tc++) {
    if (multitest && usecase)
      cout << "Case #" << tc << ": ";
    solve();
  }
}

void precomp() { return; }

int solve() {
  int h1, m1, h2, m2, k;
  cin >> h1 >> m1 >> h2 >> m2 >> k;
  int time;
  if (h1 == h2 && m1 <= m2)
    time = m2 - m1;
  else {
    time = 60 - m1;
    h1++;
    if (h1 > h2)
      h2 += 24;
    time += (h2 - h1) * 60 + m2;
  }
  cout << time - k << endl;
}