alexjercan/bugnet huggingface dataset:
Problem p01213 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
char str[310];
int dp[310][310];
int rev[310][310];
int main() {
  while (1) {
    scanf("%s", str);
    if (str[0] == '#')
      return 0;
    int ret = 0;
    string ans = "";
    int len = strlen(str);
    for (int i = 0; str[i + 1]; i++) {
      int n = i + 1;
      int m = len - n;
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= m; k++)
          dp[j][k] = -999999;
      dp[0][0] = 0;
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= m; k++) {
          if (j < n && dp[j + 1][k] < dp[j][k]) {
            dp[j + 1][k] = dp[j][k];
            rev[j + 1][k] = 1;
          }
          if (k < m && dp[j][k + 1] < dp[j][k]) {
            dp[j][k + 1] = dp[j][k];
            rev[j][k + 1] = 2;
          }
          if (j < n && k < m && dp[j + 1][k + 1] < dp[j][k] + 1 &&
              str[j] == str[i + 1 + k]) {
            dp[j + 1][k + 1] = dp[j][k] + 1;
            rev[j + 1][k + 1] = 0;
          }
        }
      if (ret < dp[n][m]) {
        ret = dp[n][m];
        string to = "";
        int tn = n;
        int tm = m;
        while (tn && tm) {
          if (rev[tn][tm] == 1)
            tn--;
          else if (rev[tn][tm] == 2)
            tm--;
          else {
            tn--;
            tm--;
            to = str[tn] + to;
          }
        }
        ans = to;
      }
    }
    // printf("%d ",ret);
    printf("%s\\n", ans.c_str());
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
char str[310];
int dp[310][310];
int rev[310][310];
int main() {
  while (1) {
    scanf("%s", str);
    if (str[0] == '#')
      return 0;
    int ret = 0;
    string ans = "";
    int len = strlen(str);
    for (int i = 0; str[i + 1]; i++) {
      int n = i + 1;
      int m = len - m;
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= m; k++)
          dp[j][k] = -999999;
      dp[0][0] = 0;
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= m; k++) {
          if (j < n && dp[j + 1][k] < dp[j][k]) {
            dp[j + 1][k] = dp[j][k];
            rev[j + 1][k] = 1;
          }
          if (k < m && dp[j][k + 1] < dp[j][k]) {
            dp[j][k + 1] = dp[j][k];
            rev[j][k + 1] = 2;
          }
          if (j < n && k < m && dp[j + 1][k + 1] < dp[j][k] + 1 &&
              str[j] == str[i + 1 + k]) {
            dp[j + 1][k + 1] = dp[j][k] + 1;
            rev[j + 1][k + 1] = 0;
          }
        }
      if (ret < dp[n][m]) {
        ret = dp[n][m];
        string to = "";
        int tn = n;
        int tm = m;
        while (tn && tm) {
          if (rev[tn][tm] == 1)
            tn--;
          else if (rev[tn][tm] == 2)
            tm--;
          else {
            tn--;
            tm--;
            to = str[tn] + to;
          }
        }
        ans = to;
      }
    }
    // printf("%d ",ret);
    printf("%s\\n", ans.c_str());
  }
}
Problem p01213 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int table[310][310];

string backtrace(const string &s, const string &t, int i, int j) {
  if (i == 0 || j == 0)
    return "";
  else if (s[i - 1] == t[j - 1]) {
    return backtrace(s, t, i - 1, j - 1) + s[i - 1];
  } else {
    if (table[i][j - 1] > table[i - 1][j])
      return backtrace(s, t, i, j - 1);
    else
      return backtrace(s, t, i - 1, j);
  }
}

string lcs(const string &s, const string &t) {
  memset(table, 0, sizeof(table));

  for (int i = 0; i <= s.length(); ++i)
    for (int j = 0; j <= t.length(); ++j)
      table[i][j] = 0;

  for (int i = 1; i <= s.length(); ++i) {
    for (int j = 1; j <= t.length(); ++j) {
      int m = (s[i - 1] == t[j - 1]) ? 1 : 0;
      int value =
          max(table[i - 1][j - 1] + m, max(table[i - 1][j], table[i][j - 1]));
      table[i][j] = value;
    }
  }

  return backtrace(s, t, s.length(), t.length());
}

string lps(string s) {
  string s1, s2, ans;
  int ansl = 0;
  for (int i = 1; i < s.length(); ++i) {
    string tmp = lcs(s.substr(0, i), s.substr(i));
    if (ans.length() < tmp.length()) {
      ans = tmp;
    }
  }

  return ans;
}

int main() {
  string input;
  while (cin >> input) {
    if (input == "#END")
      break;
    cout << lps(input) << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int table[310][310];

string backtrace(string s, string t, int i, int j) {
  if (i == 0 || j == 0)
    return "";
  else if (s[i - 1] == t[j - 1]) {
    return backtrace(s, t, i - 1, j - 1) + s[i - 1];
  } else {
    if (table[i][j - 1] > table[i - 1][j])
      return backtrace(s, t, i, j - 1);
    else
      return backtrace(s, t, i - 1, j);
  }
}

string lcs(string s, string t) {
  memset(table, 0, sizeof(table));

  for (int i = 0; i <= s.length(); ++i)
    for (int j = 0; j <= t.length(); ++j)
      table[i][j] = 0;

  for (int i = 1; i <= s.length(); ++i) {
    for (int j = 1; j <= t.length(); ++j) {
      int m = (s.at(i - 1) == t.at(j - 1)) ? 1 : 0;
      int value =
          max(table[i - 1][j - 1] + m, max(table[i - 1][j], table[i][j - 1]));
      table[i][j] = value;
    }
  }

  return backtrace(s, t, s.length(), t.length());
}

string lps(string s) {
  string s1, s2, ans;
  int ansl = 0;
  for (int i = 1; i < s.length(); ++i) {
    string tmp = lcs(s.substr(0, i), s.substr(i));
    if (ans.length() < tmp.length()) {
      ans = tmp;
    }
  }

  return ans;
}

int main() {
  string input;
  while (cin >> input) {
    if (input == "#END")
      break;
    cout << lps(input) << endl;
  }
}
Problem p01213 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int dp[305][305];
string dp2[305][305];

int main(void) {
  while (1) {
    string str;
    cin >> str;
    if (str == "#END")
      break;
    int res = 0, id = 0;
    for (int k = 1; k < (int)str.size(); k++) {
      string str1 = str.substr(0, k);
      string str2 = str.substr(k);
      memset(dp, 0, sizeof(dp));
      for (int i = 0; i < str1.size(); i++) {
        for (int j = 0; j < str2.size(); j++) {
          if (str1[i] == str2[j]) {
            dp[i + 1][j + 1] = dp[i][j] + 1;
          } else {
            dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
          }
        }
      }
      if (res < dp[str1.size()][str2.size()]) {
        res = dp[str1.size()][str2.size()];
        id = k;
      }
    }
    string str1 = str.substr(0, id);
    string str2 = str.substr(id);
    for (int i = 0; i <= str1.size(); i++) {
      for (int j = 0; j <= str2.size(); j++) {
        dp2[i][j] = "";
      }
    }
    for (int i = 0; i < str1.size(); i++) {
      for (int j = 0; j < str2.size(); j++) {
        if (str1[i] == str2[j]) {
          dp2[i + 1][j + 1] = dp2[i][j] + str1[i];
        } else {
          if (dp2[i][j + 1].size() <= dp2[i + 1][j].size())
            dp2[i + 1][j + 1] = dp2[i + 1][j];
          if (dp2[i][j + 1].size() > dp2[i + 1][j].size())
            dp2[i + 1][j + 1] = dp2[i][j + 1];
        }
      }
    }
    cout << dp2[str1.size()][str2.size()] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

string dp[301][301];

int main(void) {
  while (1) {
    string str;
    cin >> str;
    if (str == "#END")
      break;
    string res = "";
    for (int i = 1; i < (int)str.size() - 1; i++) {
      string str1 = str.substr(0, i);
      string str2 = str.substr(i);
      for (int i = 0; i < str.size(); i++) {
        for (int j = 0; j < str.size(); j++) {
          dp[i][j] = "";
        }
      }
      for (int i = 0; i < str1.size(); i++) {
        for (int j = 0; j < str2.size(); j++) {
          if (str1[i] == str2[j]) {
            if (dp[i + 1][j + 1].size() < dp[i][j].size() + 1)
              dp[i + 1][j + 1] = dp[i][j] + str1[i];
          } else {
            if (dp[i + 1][j].size() < dp[i][j].size())
              dp[i + 1][j] = dp[i][j];
            if (dp[i][j + 1].size() < dp[i][j].size())
              dp[i][j + 1] = dp[i][j];
          }
        }
      }
      if (res.size() < dp[str1.size()][str2.size()].size())
        res = dp[str1.size()][str2.size()];
    }
    cout << res << endl;
  }
  cout << "#END" << endl;
  return 0;
}
Problem p01213 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int n;
string s, dp[309][309];
int main() {
  while (cin >> s, s != "#END") {
    n = s.size();
    string ret;
    for (int i = 1; i < n; i++) {
      string l = s.substr(0, i), r = s.substr(i, n - i);
      for (int j = 1; j <= i; j++) {
        for (int k = 1; k <= n - i; k++) {
          if (l[j - 1] == r[k - 1])
            dp[j][k] = dp[j - 1][k - 1] + r[k - 1];
          else if (dp[j][k - 1].size() > dp[j - 1][k].size())
            dp[j][k] = dp[j][k - 1];
          else
            dp[j][k] = dp[j - 1][k];
        }
      }
      if (ret.size() < dp[i][n - i].size())
        ret = dp[i][n - i];
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>

using namespace std;

string dp[301][301];

inline string LCS(const char S[], const char T[]) {
  for (int i = 0; i <= 300; i++) {
    dp[0][i] = "";
    dp[i][0] = "";
  }

  int SS = strlen(S);
  int TS = strlen(T);

  for (int i = 1; i <= TS; i++) {
    for (int j = 1; j <= SS; j++) {
      if (S[j - 1] == T[i - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + T[i - 1];
      } else {
        string A = dp[i][j - 1];
        string B = dp[i - 1][j];

        if (A.size() > B.size()) {
          dp[i][j] = A;
        } else {
          dp[i][j] = B;
        }
      }
    }
  }

  return dp[TS][SS];
}

int main() {
  string STR;

  while (true) {
    cin >> STR;

    if (STR == "#END") {
      break;
    }

    string Ans = "";

    int size_ = STR.size();

    for (int i = 2; i < size_ - 1; i++) {
      string L = STR.substr(0, i);
      string R = STR.substr(i, size_);

      string A = LCS(L.c_str(), R.c_str());

      if (A.size() > Ans.size()) {
        Ans = A;
      }
    }

    printf("%s\\n", Ans.c_str());
  }

  return 0;
}
Problem p01213 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int n;
string S, dp[320][320];
int dp2[320][320];
inline string LCS(string &a1, string &a2) {
  for (int i = 0; i <= a1.size(); i++) {
    for (int j = 0; j <= a2.size(); j++)
      dp2[i][j] = -1;
  }
  dp2[0][0] = 0;
  for (int i = 0; i <= a1.size(); i++)
    dp[i][0] = "";
  for (int i = 0; i <= a2.size(); i++)
    dp[0][i] = "";
  for (int i = 0; i <= a1.size(); i++) {
    for (int j = 0; j <= a2.size(); j++) {
      if (i < a1.size() && j < a2.size() && a1[i] == a2[j]) {
        if (dp2[i + 1][j + 1] < dp2[i][j] + 1) {
          dp[i + 1][j + 1] = dp[i][j] + a1[i];
          dp2[i + 1][j + 1] = dp2[i][j] + 1;
        }
      }
      if (dp2[i + 1][j] < dp2[i][j]) {
        dp[i + 1][j] = dp[i][j];
        dp2[i + 1][j] = dp2[i][j];
      }
      if (dp2[i][j + 1] < dp2[i][j]) {
        dp[i][j + 1] = dp[i][j];
        dp2[i][j + 1] = dp2[i][j];
      }
    }
  }
  return dp[a1.size()][a2.size()];
}
int main() {
  while (cin >> S) {
    for (int i = 0; i < 102400; i++)
      dp[i / 320][i % 320] = "";
    if (S == "#END")
      break;
    n = S.size();
    string V = "";
    for (int i = 1; i < n; i++) {
      string T = S.substr(0, i), U = S.substr(i, n - i);
      string W = LCS(T, U);
      if (V.size() < W.size())
        V = W;
    }
    cout << V << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int n;
string S, dp[320][320];
int dp2[320][320];
inline string LCS(string &a1, string &a2) {
  for (int i = 0; i <= a1.size(); i++) {
    for (int j = 0; j <= a2.size(); j++)
      dp2[i][j] = -1;
  }
  dp2[0][0] = 0;
  for (int i = 0; i <= a1.size(); i++)
    dp[i][0] = "";
  for (int i = 0; i <= a2.size(); i++)
    dp[0][i] = "";
  for (int i = 0; i <= a1.size(); i++) {
    for (int j = 0; j <= a2.size(); j++) {
      if (i < a1.size() && j < a2.size() && a1[i] == a2[j]) {
        if (dp2[i + 1][j + 1] < dp2[i][j] + 1) {
          dp[i + 1][j + 1] = dp[i][j] + a1[i];
          dp2[i + 1][j + 1] = dp2[i][j] + 1;
        }
      }
      if (dp2[i + 1][j] < dp2[i][j]) {
        dp[i + 1][j] = dp[i][j];
        dp2[i + 1][j] = dp2[i][j];
      }
      if (dp2[i][j + 1] < dp2[i][j]) {
        dp[i][j + 1] = dp[i][j];
        dp2[i + 1][j] = dp2[i][j];
      }
    }
  }
  return dp[a1.size()][a2.size()];
}
int main() {
  while (cin >> S) {
    for (int i = 0; i < 102400; i++)
      dp[i / 320][i % 320] = "";
    if (S == "#END")
      break;
    n = S.size();
    string V = "";
    for (int i = 1; i < n; i++) {
      string T = S.substr(0, i), U = S.substr(i, n - i);
      string W = LCS(T, U);
      if (V.size() < W.size())
        V = W;
    }
    cout << V << endl;
  }
  return 0;
}
Problem p01220 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P;
double eps = 1e-6;
double PI = acos(-1);

bool eq(double a, double b) { return (-eps < a - b && a - b < eps); }

P getCrossPoint(P a, P b, P c, P d) {
  a -= d;
  b -= d;
  c -= d;
  return d + a + (b - a) * imag(a / c) / imag(a / c - b / c);
}

bool isParallel(P a, P b, P c, P d) {
  return eq(0, imag((b - a) * conj(d - c)));
}

bool onSegment(P a, P b, P p) {
  return eq(abs(b - a), abs(p - a) + abs(p - b));
}

int lcm(int a, int b) { return a / __gcd(a, b) * b; }

int n;

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    double R = 1.0 / sqrt(3.0);
    P a = P(R, 0);
    P b = P(R - 0.5 * sqrt(3.0), 0.5);

    double lc = lcm(n, 3);
    double ti = 2.0 * PI / lc;

    P c = a, d = b;

    if (lc == 3) {
      printf("0.433013\\n");
      continue;
    }

    double T = 1e9;
    for (int i = 0; i < n; i++) {
      c *= P(cos(ti), sin(ti));
      d *= P(cos(ti), sin(ti));
      if (!isParallel(a, b, c, d)) {
        P target = getCrossPoint(a, b, c, d);
        double k = min(abs(target - a), abs(target - b));
        if (eq(k, 0))
          continue;
        T = min(T, k);
      }
    }
    double ans = T * R * 0.5;
    printf("%.8f\\n", ans * lc);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P;
double eps = 1e-8;
double PI = acos(-1);

bool eq(double a, double b) { return (-eps < a - b && a - b < eps); }

P getCrossPoint(P a, P b, P c, P d) {
  a -= d;
  b -= d;
  c -= d;
  return d + a + (b - a) * imag(a / c) / imag(a / c - b / c);
}

bool isParallel(P a, P b, P c, P d) {
  return eq(0, imag((b - a) * conj(d - c)));
}

bool onSegment(P a, P b, P p) {
  return eq(abs(b - a), abs(p - a) + abs(p - b));
}

void func(vector<P> &vec, P p) {
  for (int i = 0; i < (int)vec.size(); i++) {
    if (abs(vec[i] - p) < eps)
      return;
  }
  vec.push_back(p);
}

int n;

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    double R = 1.0 / sqrt(3.0);
    P s120 = P(-0.5, sqrt(3.0) / 2.0);
    P a = P(0, R);
    P b = P(0.5, R - 0.5 * sqrt(3.0));
    P target = P(0, -10);

    vector<P> vec;

    for (int i = 0; i < n; i++) {
      double ti = 2.0 * PI / (double)n;
      ti *= (double)i;
      P c = a * P(cos(ti), sin(ti));
      P d = b * P(cos(ti), sin(ti));

      for (int j = 0; j < 3; j++) {
        c *= s120;
        d *= s120;
        func(vec, c);
        func(vec, d);
        if (isParallel(a, b, c, d))
          continue;
        P p = getCrossPoint(a, b, c, d);
        if (!onSegment(a, b, p) || !onSegment(c, d, p))
          continue;
        if (p.imag() < R - eps && p.imag() > target.imag())
          target = p;
      }
    }

    double T = abs(a - target);
    if (eq(T, 1)) {
      printf("0.433013\\n");
      continue;
    }
    double ans = T * R * 0.5;
    printf("%.8f\\n", ans * (double)vec.size());
  }
}
Problem p01223 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#define max(x, y) x > y ? x : y
int main() {
  int a, b, c, i, umax, dmax;
  scanf("%d", &i);
  for (; i > 0; i--) {
    umax = 0;
    dmax = 0;
    scanf("%d", &a);
    scanf("%d", &b);
    for (int i = 1; i < a; i++) {
      scanf("%d", &c);
      if (b < c)
        umax = max(umax, c - b);
      else
        dmax = max(dmax, b - c);
      b = c;
    }
    printf("%d %d\\n", umax, dmax);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#define max(x, y) x > y ? x : y
int main() {
  int a, b, c, umax, dmax;
  while (1) {
    umax = 0;
    dmax = 0;
    scanf("%d", &a);
    if (a == 0)
      return 0;
    scanf("%d", &b);
    for (int i = 1; i < a; i++) {
      scanf("%d", &c);
      if (b < c)
        umax = max(umax, c - b);
      else
        dmax = max(dmax, b - c);
      b = c;
    }
    printf("%d %d\\n", umax, dmax);
  }
}
Problem p01223 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>

using namespace std;

typedef pair<int, int> P;

P solve(int n, int h[]) {
  P answer(0, 0);
  for (int i = 0; i < n - 1; ++i) {
    if (h[i] < h[i + 1]) {
      answer.first = max(answer.first, h[i + 1] - h[i]);
    }
    if (h[i] > h[i + 1]) {
      answer.second = max(answer.second, h[i] - h[i + 1]);
    }
  }
  return answer;
}

int main() {
  int t, n;
  int h[100];
  P answer;
  cin >> t;
  for (int case_num = 0; case_num < t; ++case_num) {
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> h[i];
    }
    answer = solve(n, h);
    cout << answer.first << ' ' << answer.second << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>

using namespace std;

typedef pair<int, int> P;

P solve(int n, int h[]) {
  P answer(0, 0);
  for (int i = 0; i < n - 1; ++i) {
    if (h[i] < h[i + 1]) {
      answer.first = max(answer.first, h[i + 1] - h[i]);
    }
    if (h[i] > h[i + 1]) {
      answer.second = max(answer.second, h[i] - h[i + 1]);
    }
  }
  return answer;
}

int main() {
  int t, n;
  int h[100];
  P answer;
  cin >> t;
  for (int case_num = 0; case_num < t; ++t) {
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> h[i];
    }
    answer = solve(n, h);
    cout << answer.first << ' ' << answer.second << endl;
  }
  return 0;
}
Problem p01223 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define REP(i, n, m) for (i = n; i < m; i++)
#define rep(i, n) REP(i, 0, n)
int main() {

  int i, j, n, m, sai;
  int a[1000], up[1000], down[1000];
  int h = 0, k = 0, maxup, maxdown;

  scanf("%d", &n);
  rep(i, n) {
    scanf("%d", &m);
    rep(j, m) { scanf("%d", &a[j]); }
    rep(j, m - 1) {
      sai = a[j + 1] - a[j];
      if (sai > 0) {
        if (k == 0) {
          maxup = sai;
        } else {
          if (sai > maxup) {
            maxup = sai;
          }
        }
        k++;
      } else if (sai < 0) {
        if (h == 0) {
          maxdown = sai;
        } else {
          if (sai < maxdown) {
            maxdown = sai;
          }
        }
        h++;
      }
    }

    printf("%d %d\\n", maxup, -1 * maxdown);
    maxup = 0, maxdown = 0;
    k = 0, h = 0;
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
#define REP(i, n, m) for (i = n; i < m; i++)
#define rep(i, n) REP(i, 0, n)
int main() {

  int i, j, n, m, sai;
  int a[1000], up[1000], down[1000], dan[1000];
  int h = 0, k = 0, s = 0, maxup, maxdown;

  scanf("%d", &n);
  rep(i, n) {
    scanf("%d", &m);
    rep(j, m) { scanf("%d", &a[j]); }
    rep(j, m - 1) {
      sai = a[j + 1] - a[j];
      if (sai > 0) {
        up[k] = sai;
        k++;
      } else if (sai < 0) {
        down[h] = sai;
        h++;
      }
    }

    rep(j, k) {
      if (j == 0) {
        maxup = up[j];
      } else {
        if (up[j] > maxup) {
          maxup = up[j];
        }
      }
    }

    rep(j, h) {
      if (j == 0) {
        maxdown = down[j];
      } else {
        if (down[j] < maxdown) {
          maxdown = down[j];
        }
      }
    }
    printf("%d %d\\n", maxup, -1 * maxdown);
    rep(j, 1000) {
      up[j] = 0;
      down[j] = 0;
    }
  }
}
Problem p01223 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main() {

  int t;
  int n;
  int h[100];
  int d_max = 0;
  int u_max = 0;
  int tmp = 0;

  while (1) {
    scanf("%d", &t);
    if (0 < t && t <= 100) {
      break;
    }
  }
  for (int i = 0; i < t; i++) {
    while (1) {
      scanf("%d", &n);
      if (2 <= n && n <= 100) {
        break;
      }
    }

    for (int j = 0; j < n; j++) {
      while (1) {
        scanf("%d", &h[j]);
        if (0 < h[j] && h[j] <= 1000) {
          break;
        }
      }
    }

    for (int k = 1; k < n; k++) {

      if (u_max < (h[k] - h[k - 1])) {
        u_max = h[k] - h[k - 1];
      }

      if (d_max < (h[k - 1] - h[k])) {
        d_max = h[k - 1] - h[k];
      }
    }
    printf("%d %d\\n", u_max, d_max);
    u_max = 0;
    d_max = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {

  int t;
  int n;
  int h[100];
  int d_max = 0;
  int u_max = 0;
  int tmp = 0;

  while (1) {
    scanf("%d", &t);
    if (0 < t && t <= 100) {
      break;
    }
  }
  for (int i = 0; i < t; t++) {
    while (1) {
      scanf("%d", &n);
      if (2 <= n && n <= 100) {
        break;
      }
    }

    for (int j = 0; j < n; j++) {
      while (1) {
        scanf("%d", &h[j]);
        if (0 < h[j] && h[j] <= 1000) {
          break;
        }
      }
    }

    for (int k = 1; k < n; k++) {
      // printf("%d , %d\\n",u_max,h[k]-h[k-1]);
      // printf("%d , %d\\n",d_max,h[k]-h[k+1]);

      if (u_max < (h[k] - h[k - 1])) {
        u_max = h[k] - h[k - 1];
      }
      if (k < n - 1) {
        if (d_max < (h[k] - h[k + 1])) {
          d_max = h[k] - h[k + 1];
        }
      }
    }
    printf("%d %d\\n", u_max, d_max);
    u_max = 0;
    d_max = 0;
  }

  return 0;
}
Problem p01223 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }

int main(void) {

  int t, n, h[101], i, up = 0, down = 0;

  scanf("%d", &t);

  while (t--) {
    scanf("%d", &n);
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = up = down = 0; i < n - 1; i++) {
      up = max(up, h[i + 1] - h[i]);
      down = min(down, h[i + 1] - h[i]);
    }
    printf("%d %d\\n", up, -down);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }

int main(void) {

  int t, n, h[101], i, up = 0, down = 0;

  scanf("%d", &t);

  while (t--, scanf("%d", &n)) {
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = up = down = 0; i < n - 1; i++) {
      up = max(up, h[i + 1] - h[i]);
      down = min(down, h[i + 1] - h[i]);
    }
    printf("%d %d\\n", up, -down);
  }
  return 0;
}