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

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
#undef max
#undef min

constexpr int INF = 99999999;
std::unordered_map<int, int> drawTable; // key:カードの数字 value:引く順番

int markNumber(std::vector<std::vector<int>> &mat, int minBingo) {
  std::vector<std::vector<int>> openTable;
  std::vector<int> eachMinBingoTurn;

  int bingoCount = 0;
  for (auto &line : mat) {
    openTable.push_back(std::vector<int>());
    for (auto &x : line) {
      auto order = drawTable.find(x);
      if (order != drawTable.end()) {
        openTable.back().push_back(order->second);
      } else
        openTable.back().push_back(INF);
    }
  }

  // 横方向のビンゴ
  for (int i = 0; i < openTable.size(); i++) {
    int minTurn = 0;
    for (int j = 0; j < openTable[i].size(); j++) {
      minTurn = std::max(minTurn, openTable[i][j]);
    }
    eachMinBingoTurn.push_back(minTurn);
  }

  if (mat.size() > 1) {
    // 縦方向のビンゴ
    for (int i = 0; i < openTable.size(); i++) {
      int minTurn = 0;
      for (int j = 0; j < openTable[i].size(); j++) {
        minTurn = std::max(minTurn, openTable[j][i]);
      }
      eachMinBingoTurn.push_back(minTurn);
    }

    // ななめ方向のビンゴ
    int minTurn = 0;
    for (int i = 0; i < openTable.size(); i++) {
      minTurn = std::max(minTurn, openTable[i][i]);
    }
    eachMinBingoTurn.push_back(minTurn);

    // ななめ方向のビンゴ
    minTurn = 0;
    for (int i = 0; i < openTable.size(); i++) {
      minTurn = std::max(minTurn, openTable[i][openTable.size() - i - 1]);
    }
    eachMinBingoTurn.push_back(minTurn);
  }

  std::sort(eachMinBingoTurn.begin(), eachMinBingoTurn.end());

  if (minBingo > eachMinBingoTurn.size())
    return INF;

  return eachMinBingoTurn[minBingo - 1];
}

int main() {
  int n, u, v, m;
  std::vector<std::vector<int>> usaMat, nekoMat;
  std::vector<int> drawCards;

  std::cin >> n >> u >> v >> m;

  for (int i = 0; i < n; i++) {
    usaMat.push_back(std::vector<int>());
    for (int j = 0; j < n; j++) {
      int a;
      std::cin >> a;
      usaMat.back().push_back(a);
    }
  }

  for (int i = 0; i < n; i++) {
    nekoMat.push_back(std::vector<int>());
    for (int j = 0; j < n; j++) {
      int a;
      std::cin >> a;
      nekoMat.back().push_back(a);
    }
  }

  drawTable.reserve(100000);
  for (int i = 0; i < m; i++) {
    int a;
    std::cin >> a;
    drawTable.insert(std::make_pair(a, i));
  }

  int nunUsaTurn = markNumber(usaMat, u);
  int nunNekoTurn = markNumber(nekoMat, v);

  if (nunUsaTurn < nunNekoTurn) {
    std::cout << "USAGI" << std::endl;
    return 0;
  } else if (nunNekoTurn < nunUsaTurn) {
    std::cout << "NEKO" << std::endl;
    return 0;
  } else if (nunNekoTurn == nunUsaTurn) {
    std::cout << "DRAW" << std::endl;
    return 0;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

struct BingoInfo {
  int h[500] = {0};
  int v[500] = {0};
  int s[2] = {0};
};

int markNumber(std::vector<std::vector<int>> &mat, int number,
               BingoInfo &bingoInfo, int n) {
  int bingoCount = 0;
  for (int i = 0; i < mat.size(); i++) {
    for (int j = 0; j < mat[i].size(); j++) {
      if (mat[i][j] == number) {
        mat[i][j] = -1;
        bingoInfo.h[j]++;
        bingoInfo.v[i]++;
        if (i == j)
          bingoInfo.s[0]++;
        if (i == mat[i].size() - j - 1) {
          bingoInfo.s[1]++;
        }
        if (bingoInfo.h[j] == n)
          bingoCount++;
        if (bingoInfo.v[i] == n)
          bingoCount++;
      }
    }
  }
  if (n == 1 && bingoCount > 0)
    return 1;
  return bingoCount;
}

int countBingoLines(const BingoInfo &bingoInfo, int n) {

  int numBingo = 0;
  for (const auto &x : bingoInfo.s) {
    if (x == n)
      numBingo++;
  }
  return numBingo;
}

int main() {
  int n, u, v, m;
  std::vector<std::vector<int>> usaMat, nekoMat;
  BingoInfo usaBingoInfo, nekoBingoInfo;
  int usaBingoCount = 0, nekoBingoCount = 0;
  std::vector<int> drawCards;

  std::cin >> n >> u >> v >> m;

  for (int i = 0; i < n; i++) {
    usaMat.push_back(std::vector<int>());
    for (int j = 0; j < n; j++) {
      int a;
      std::cin >> a;
      usaMat.back().push_back(a);
    }
  }

  for (int i = 0; i < n; i++) {
    nekoMat.push_back(std::vector<int>());
    for (int j = 0; j < n; j++) {
      int a;
      std::cin >> a;
      nekoMat.back().push_back(a);
    }
  }

  for (int i = 0; i < m; i++) {
    int a;
    std::cin >> a;
    drawCards.push_back(a);
  }

  for (auto card : drawCards) {
    usaBingoCount += markNumber(usaMat, card, usaBingoInfo, n);
    nekoBingoCount += markNumber(nekoMat, card, nekoBingoInfo, n);
    int usaSlantBingo = countBingoLines(usaBingoInfo, n);
    int nekoSlantBingo = countBingoLines(nekoBingoInfo, n);
    int numUsaBingo = usaBingoCount + usaSlantBingo;
    int numNekoBingo = nekoBingoCount + nekoSlantBingo;

    if (n == 1) {
      numUsaBingo = numUsaBingo >= 1 ? 1 : 0;
      numNekoBingo = numNekoBingo >= 1 ? 1 : 0;
    }

    if (numUsaBingo >= u && numNekoBingo < v) {
      std::cout << "USAGI" << std::endl;
      return 0;
    } else if (numNekoBingo >= v && numUsaBingo < u) {
      std::cout << "NEKO" << std::endl;
      return 0;
    } else if (numNekoBingo >= v && numUsaBingo >= u) {
      std::cout << "DRAW" << std::endl;
      return 0;
    }
  }
  std::cout << "DRAW" << std::endl;
  return 0;
}