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

#include <cstring>
#include <iostream>

using namespace std;

const int Height = 5100;
const int Width = 5;
char field[Height][Width];
int top, ans;
int dx[] = {1, 0};
int dy[] = {0, -1};

void print(int h, int w) {

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cout << field[i][j];
    cout << endl;
  }
  cout << endl;
}

bool isInside(int h, int w) {
  return 0 <= h && h < Height && 0 <= w && w < Width;
}

int getH(int d, int len, int pos) {
  for (int i = top + 10; i >= 0; i--) {
    int h = i;
    int w = pos;
    for (int j = 0; j < len; j++) {
      if (field[h][w] != '?' || !isInside(h, w))
        return i + 1;
      h += dy[d];
      w += dx[d];
    }
  }
  return 0;
}

void down(int n) {

  for (int i = n; i <= top + 6; i++)
    for (int j = 0; j < Width; j++)
      field[i][j] = field[i + 1][j];
  // top--;
}

void erase() {

  for (int i = 0; i <= top + 6; i++) {
    bool f = true;
    for (int j = 0; j < Width; j++)
      if (field[i][j] == '?')
        f = false;
    if (f) {
      ans -= 5;
      down(i--);
    }
  }
}

void put(int d, int len, int pos) {

  int nex = getH(d, len, pos);
  top = max(top, nex);

  int h = nex;
  int w = pos;

  for (int i = 0; i < len; i++) {
    field[h][w] = '.';
    h += dy[d];
    w += dx[d];
  }
}

int main() {

  int n;
  while (cin >> n && n) {
    memset(field, '?', sizeof(field));
    ans = top = 0;

    while (n--) {
      int d, len, pos;
      cin >> d >> len >> pos;
      ans += len;
      put(d - 1, len, pos - 1);
      erase();
    }
    cout << ans << endl;
    //  print(10,5);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

const int Height = 5500;
const int Width = 5;
char field[Height][Width];
int top, ans;
int dx[] = {1, 0};
int dy[] = {0, -1};

void print(int h, int w) {

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cout << field[i][j];
    cout << endl;
  }
  cout << endl;
}
bool isInside(int h, int w) {
  return 0 <= h && h < Height && 0 <= w && w < Width;
}

int getH(int d, int len, int pos) {
  for (int i = Height - 1; i >= 0; i--) {
    int h = i;
    int w = pos;
    for (int j = 0; j < len; j++) {
      if (field[h][w] != '?' || !isInside(h, w))
        return i + 1;
      h += dy[d];
      w += dx[d];
    }
  }
}

void down(int n) {

  for (int i = n; i < Height; i++)
    for (int j = 0; j < Width; j++)
      field[i][j] = field[i + 1][j];
}

void erase() {

  for (int i = 0; i <= top; i++) {
    bool f = true;
    for (int j = 0; j < Width; j++)
      if (field[i][j] == '?')
        f = false;
    if (f) {
      ans -= 5;
      down(i);
      i--;
    }
  }
}

void put(int d, int len, int pos) {

  int nex = getH(d, len, pos);
  top = max(top, nex);

  int h = nex;
  int w = pos;

  for (int i = 0; i < len; i++) {
    field[h][w] = '.';
    h += dy[d];
    w += dx[d];
  }
}

int main() {

  int n;
  while (cin >> n && n) {
    memset(field, '?', sizeof(field));
    ans = top = 0;

    while (n--) {
      int d, len, pos;
      cin >> d >> len >> pos;
      ans += len;
      put(d - 1, len, pos - 1);
      erase();
    }
    cout << ans << endl;
    print(10, 5);
  }
  return 0;
}