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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

#define MAX 1000000

int main(void) {
  int e;
  vector<pii> vs;
  for (int y = 0;; y++) {
    int k = y * y;
    if (k > MAX)
      break;
    for (int z = 0;; z++) {
      int l = z * z * z;
      if (k + l > MAX)
        break;
      vs.push_back(pii(k + l, y + z));
    }
  }
  sort(ALL(vs));

  while (cin >> e, e) {
    int ans = (1 << 29);
    rep(i, vs.size()) {
      int x = e - vs[i].first;
      if (x < 0)
        continue;
      ans = min(ans, x + vs[i].second);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

#define MAX 1000000

int main(void) {
  int e;
  vector<pii> vs;
  for (int y = 0;; y++) {
    int k = y * y;
    if (k > MAX)
      break;
    for (int z = 0;; z++) {
      int l = z * z * z;
      if (k + l > MAX)
        break;
      vs.push_back(pii(k + l, y + z));
    }
  }
  sort(ALL(vs));

  while (cin >> e, e) {
    int ans = (1 << 29);
    for (int x = 0; x <= max(e / 3 + 1, 3); x++) {
      int e_x = e - x;

      int a = 0, b = vs.size();
      while (b - a > 1) {
        int m = (a + b) / 2;
        if (vs[m].first >= e_x)
          b = m;
        else
          a = m;
      }
      if (vs[b].first == e_x)
        ans = min(ans, x + vs[b].second);
    }
    cout << ans << endl;
  }
  return 0;
}