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

#include <cassert>
#include <climits>
#include <fstream>
#include <iostream>
#include <stdint.h>
#include <stdio.h>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...)                                                           \\
  do {                                                                         \\
    printf("D(L%d) %s: ", __LINE__, __func__);                                 \\
    printf(__VA_ARGS__);                                                       \\
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG

struct Matrix {
  int numOfRow;
  int numOfColumn;
};

struct MinMatrix {
  int minMultiply;
  int numOfRow;
  int numOfColumn;
};

const int kMaxSize = 100;
const int kInvalid = -1;
MinMatrix minArrays[kMaxSize][kMaxSize];

bool isValid(MinMatrix mtx) {
  return mtx.minMultiply != kInvalid ? true : false;
}

MinMatrix getMinScalarMultiply(const Matrix matrices[], int start, int end) {
  DPRINTF("start %d  end %d\\n", start, end);

  assert(start <= end);

  if (isValid(minArrays[start][end]))
    return minArrays[start][end];

  if (start + 1 == end) {
    MinMatrix mtx;

    mtx.minMultiply = 0;
    mtx.numOfRow = matrices[start].numOfRow;
    mtx.numOfColumn = matrices[start].numOfColumn;

    minArrays[start][end] = mtx;

    return minArrays[start][end];
  }

  MinMatrix minMtx;
  minMtx.minMultiply = INT_MAX;

  for (int i = start + 1; i < end; i++) {
    MinMatrix mtx0 = getMinScalarMultiply(matrices, start, i);
    MinMatrix mtx1 = getMinScalarMultiply(matrices, i, end);

    MinMatrix mtx;

    mtx.minMultiply = mtx0.minMultiply + mtx1.minMultiply +
                      mtx0.numOfRow * mtx0.numOfColumn * mtx1.numOfColumn;
    mtx.numOfRow = mtx0.numOfRow;
    mtx.numOfColumn = mtx1.numOfColumn;

#if DEBUG
    DPRINTF("start %d  end %d  i %d : mtx.minMultiply %d  min.minMultiply %d\\n",
            start, end, i, mtx.minMultiply, minMtx.minMultiply);
    DPRINTF("  mtx0.minMultiply %d  mtx1.minMultiply %d\\n", mtx0.minMultiply,
            mtx1.minMultiply);
#endif

    if (minMtx.minMultiply > mtx.minMultiply)
      minMtx = mtx;
  }

  DPRINTF("start %d  end %d  multiply %d\\n", start, end, minMtx.minMultiply);

  minArrays[start][end] = minMtx;

  return minArrays[start][end];
}

int calcMinScalarMultiply(const Matrix matrices[], int num) {
  if (num <= 1)
    return 0;

  MinMatrix mtx = getMinScalarMultiply(matrices, 0, num);

  return mtx.minMultiply;
}

int main() {

  // init
  for (int i = 0; i < kMaxSize; i++)
    for (int j = 0; j < kMaxSize; j++)
      minArrays[i][j].minMultiply = kInvalid;

  int numOfMatrix;
  cin >> numOfMatrix;
  cin.ignore();

  Matrix matrices[numOfMatrix];
  for (int i = 0; i < numOfMatrix; i++) {
    cin >> matrices[i].numOfRow;
    cin >> matrices[i].numOfColumn;
    cin.ignore();
  }

#if DEBUG
  for (int i = 0; i < numOfMatrix; i++) {
    DPRINTF("Matrix %d: %d %d\\n", i, matrices[i].numOfRow,
            matrices[i].numOfColumn);
  }
#endif

  cout << calcMinScalarMultiply(matrices, numOfMatrix) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cassert>
#include <climits>
#include <fstream>
#include <iostream>
#include <stdint.h>
#include <stdio.h>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...)                                                           \\
  do {                                                                         \\
    printf("D(L%d) %s: ", __LINE__, __func__);                                 \\
    printf(__VA_ARGS__);                                                       \\
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG

struct Matrix {
  int numOfRow;
  int numOfColumn;
};

struct MinMatrix {
  int minMultiply;
  int numOfRow;
  int numOfColumn;
};

MinMatrix getMinScalarMultiply(const Matrix matrices[], int start, int end) {
  DPRINTF("start %d  end %d\\n", start, end);

  assert(start < end);

  if (start >= end)
    return MinMatrix();

  if (start + 1 == end) {
    MinMatrix mtx;

    mtx.minMultiply = 0;
    mtx.numOfRow = matrices[start].numOfRow;
    mtx.numOfColumn = matrices[start].numOfColumn;

    return mtx;
  }

  MinMatrix minMtx;
  minMtx.minMultiply = INT_MAX;

  for (int i = start + 1; i < end; i++) {
    MinMatrix mtx0 = getMinScalarMultiply(matrices, start, i);
    MinMatrix mtx1 = getMinScalarMultiply(matrices, i, end);

    MinMatrix mtx;

    mtx.minMultiply = mtx0.minMultiply + mtx1.minMultiply +
                      mtx0.numOfRow * mtx0.numOfColumn * mtx1.numOfColumn;
    mtx.numOfRow = mtx0.numOfRow;
    mtx.numOfColumn = mtx1.numOfColumn;

#if DEBUG
    DPRINTF("start %d  end %d  i %d : mtx.minMultiply %d  min.minMultiply %d\\n",
            start, end, i, mtx.minMultiply, minMtx.minMultiply);
    DPRINTF("  mtx0.minMultiply %d  mtx1.minMultiply %d\\n", mtx0.minMultiply,
            mtx1.minMultiply);
#endif

    if (minMtx.minMultiply > mtx.minMultiply)
      minMtx = mtx;
  }

  DPRINTF("start %d  end %d  multiply %d\\n", start, end, minMtx.minMultiply);

  return minMtx;
}

int calcMinScalarMultiply(const Matrix matrices[], int num) {
  if (num <= 1)
    return 0;

  MinMatrix mtx = getMinScalarMultiply(matrices, 0, num);

  return mtx.minMultiply;
}

int main() {
  int numOfMatrix;
  cin >> numOfMatrix;
  cin.ignore();

  Matrix matrices[numOfMatrix];
  for (int i = 0; i < numOfMatrix; i++) {
    cin >> matrices[i].numOfRow;
    cin >> matrices[i].numOfColumn;
    cin.ignore();
  }

#if DEBUG
  for (int i = 0; i < numOfMatrix; i++) {
    DPRINTF("Matrix %d: %d %d\\n", i, matrices[i].numOfRow,
            matrices[i].numOfColumn);
  }
#endif

  cout << calcMinScalarMultiply(matrices, numOfMatrix) << endl;

  return 0;
}