Problem p02266 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

#define MAX 40002
#define BASE 20000

int a[MAX];
int sum[MAX];
int sz;
int left[MAX];

int main() {
  int i, j, k, x, h, s, ans;
  int c;

  h = BASE, ans = s = 0;
  for (x = 1;; x++) {
    c = gc();
    if (c <= ' ')
      break;
    if (c == '\\\\') {
      if (s > 0)
        ans += s, sum[sz++] = s, s = 0;
      a[--h] = x;
    } else if (c == '/') {
      if (a[h] > 0)
        left[sz] = a[h], s += x - a[h];
      else if (s > 0)
        ans += s, sum[sz++] = s, s = 0;
      h++;
    }
  }
  if (s > 0)
    ans += s, sum[sz++] = s;

  printf("%d\\n", ans);
  k = sz;
  i = sz - 1;
  while (i > 0) {
    j = i - 1;
    while (j >= 0 && left[j] > left[i]) {
      sum[i] += sum[j], sum[j] = 0, j--, k--;
    }
    i = j;
  }

  printf("%d", k);
  for (i = 0; i < sz; i++)
    if (sum[i] > 0)
      printf(" %d", sum[i]);
  putchar('\\n');
  return 0;
}


Error: MLE

Fixed submission:

#include <stdio.h>

#define STR 20000

char lake[2 * STR][STR];   // 湖
int lakes[STR] = {0};      // 小さい湖
int sl[STR];               // 湖カウント始点
int d = 0, lc = 0, ll = 0; // d:深さ　lc:湖数 ll:湖総量

// 小さい湖・総量の算出
void LakeCount(int ic, int i, int left) { //
  int count, s;

  //    printf("ic=%d sl[d]=%d d=%d lc=%d left=%d:LakeC\\n",ic,sl[d],d,lc,left);
  s = sl[d];
  for (int k = ic; k <= i; k++) {
    count = 0;

    for (int a = sl[d]; a <= left; a++) {
      if (lake[k][a] == '\\\\') {
        while (lake[k][a] != '/') {
          count++;
          a++;
        }
      }
    }

    //        printf("count =%d\\n",count);

    lakes[lc] += count;
    ll += count;

    d--;
  }
}

// 水深の算出
void DeepCount(int i, int start, int k) {
  int st, ic = i;

  //  printf("%d %d %d %d :DeepC\\n", i, start, k, lc);
  sl[d] = start;
  for (st = start; st <= k; st++) {
    if (lake[ic - 1][st + 1] == '\\\\') {
      sl[d + 1] = st + 1;
      d++;
      ic--;
    } else if (lake[ic][st + 1] == '/') {
      //        printf("ic=%d st=%d :DeepC1\\n",ic,st);
      LakeCount(ic, i, k);
      break;
    }

    //    printf("i=%d st=%d :DeepC\\n",i,st);
  }

  d = 0;
}

// 湖の形成と水面算出と出力
int main(void) {
  char a;
  int i, j, k, c = 0, n = 0, min = STR, max = 0;

  for (i = 0; i < 2 * STR; i++) {
    for (j = 0; j < STR; j++) {
      lake[i][j] = ' ';
    }
  }

  i = STR;
  j = 0;

  // 文字列入力
  while ((a = getchar()) != EOF) {
    if (a == '/') {
      lake[i][j] = a;
      i++;
    } else if (a == '_') {
      lake[i][j] = a;
    } else {
      i--;
      lake[i][j] = a;
    }

    if (i > max) {
      max = i;
    }
    if (i < min) {
      min = i;
    }
    j++;
    n++;
  }

  // printf("%c\\n",lake[200][9]);

  // 水面の検知
  for (j = 0; j < n; j++) {
    for (i = max; i > min; i--) {
      if (lake[i][j] == '\\\\') {
        for (k = j; k < n; k++) {
          if (lake[i][k] == '/') {
            c++;
            lc = j;
            DeepCount(i, j, k);
            j = k;
            break;
          }
        }
      }
    }
  }

  // 小湖・湖の総量出力
  printf("%d\\n", ll);
  printf("%d ", c);
  for (int i = 0; i < n; i++) {
    if (lakes[i] != 0) {
      printf("%d", lakes[i]);
      if (i != lc - 1) {
        printf(" ");
      }
    }
  }
  printf("\\n");

  /*
  //湖の出力
  printf("%d %d\\n",max,min);
    for(i = max ; i > min ; i--) {
      for(j = 0 ; j < n; j++){
        printf("%c",lake[i-1][j]);
      }
      printf("\\n");
    }
  */

  return 0;
}