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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;
// Bottom, Front, Left, Right, Back, Top
// 0     , 1    , 2   , 3    , 4   , 5
int T[6];

bool valid() {
  if (T[0] < 0)
    return false;

  int sum = 0;
  REP(i, 6) sum += T[i];

  if (2 * (T[0] + T[5]) > sum)
    return false;
  if (2 * (T[1] + T[4]) - 1 > sum)
    return false;
  if (2 * (T[2] + T[3]) - 1 > sum)
    return false;
  return true;
}

// a, b, c, d -> b, c, d, a
inline void roll(int a, int b, int c, int d) {
  int tmp = T[a];
  T[a] = T[b];
  T[b] = T[c];
  T[c] = T[d];
  T[d] = tmp;
}

inline void rollN() { roll(0, 4, 5, 1); }
inline void rollS() { roll(1, 5, 4, 0); }
inline void rollW() { roll(5, 2, 0, 3); }
inline void rollE() { roll(3, 0, 2, 5); }

inline void rollX(char c) {
  if (c == 'N')
    rollN();
  else if (c == 'S')
    rollS();
  else if (c == 'W')
    rollW();
  else if (c == 'E')
    rollE();
}

inline void roll_backX(char c) {
  if (c == 'S')
    rollN();
  else if (c == 'N')
    rollS();
  else if (c == 'E')
    rollW();
  else if (c == 'W')
    rollE();
}

bool is_end() {
  REP(i, 6) if (T[i] != 0) return false;
  return true;
}

char ans[55555];
int LEN;
bool dfs(int idx) {
  if (idx == LEN)
    return true;
  // E, N, S, W
  {
    rollE();
    T[0]--;
    if (valid()) {
      ans[idx] = 'E';
      if (dfs(idx + 1)) {
        return true;
      }
    }
    T[0]++;
    rollW();
  }
  {
    rollN();
    T[0]--;
    if (valid()) {
      ans[idx] = 'N';
      if (dfs(idx + 1)) {
        return true;
      }
    }
    T[0]++;
    rollS();
  }
  {
    rollS();
    T[0]--;
    if (valid()) {
      ans[idx] = 'S';
      if (dfs(idx + 1)) {
        return true;
      }
    }
    T[0]++;
    rollN();
  }
  {
    rollW();
    T[0]--;
    if (valid()) {
      ans[idx] = 'W';
      if (dfs(idx + 1)) {
        return true;
      }
    }
    T[0]++;
    rollW();
  }
  return false;
}

int main() {
  while (true) {
    vector<int> t(6);
    REP(i, 6) cin >> t[i];

    REP(i, 6) T[i] = t[i];
    if (is_end())
      break;
    LEN = accumulate(t.begin(), t.end(), 0);

    int p, q;
    cin >> p >> q;
    p--;

    string answer = "Z";

    sort(t.begin(), t.end());
    do {
      REP(i, 6) T[i] = t[i];
      bool res = dfs(0);
      if (res) {
        string cand;
        REP(i, LEN) cand += ans[i];
        answer = min(answer, cand);
      }
    } while (next_permutation(t.begin(), t.end()));

    if (answer == "Z") {
      cout << "impossible" << endl;
    } else {
      for (int i = p; i < q; i++) {
        cout << answer[i];
      }
      cout << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;
// Bottom, Front, Left, Right, Back, Top
// 0     , 1    , 2   , 3    , 4   , 5
int T[6];

bool valid() {
  if (T[0] < 0)
    return false;

  int sum = 0;
  REP(i, 6) sum += T[i];

  if (2 * (T[0] + T[5]) > sum)
    return false;
  if (2 * (T[1] + T[4]) - 1 > sum)
    return false;
  if (2 * (T[2] + T[3]) - 1 > sum)
    return false;
  return true;
}

// a, b, c, d -> b, c, d, a
inline void roll(int a, int b, int c, int d) {
  int tmp = T[a];
  T[a] = T[b];
  T[b] = T[c];
  T[c] = T[d];
  T[d] = tmp;
}

inline void rollN() { roll(0, 4, 5, 1); }
inline void rollS() { roll(1, 5, 4, 0); }
inline void rollW() { roll(5, 2, 0, 3); }
inline void rollE() { roll(3, 0, 2, 5); }

inline void rollX(char c) {
  if (c == 'N')
    rollN();
  else if (c == 'S')
    rollS();
  else if (c == 'W')
    rollW();
  else if (c == 'E')
    rollE();
}

inline void roll_backX(char c) {
  if (c == 'S')
    rollN();
  else if (c == 'N')
    rollS();
  else if (c == 'E')
    rollW();
  else if (c == 'W')
    rollE();
}

bool is_end() {
  REP(i, 6) if (T[i] != 0) return false;
  return true;
}

char ans[55555];
int LEN;
bool dfs(int idx) {
  if (idx == LEN)
    return true;
  // E, N, S, W
  const string cand = "ENSW";
  for (char c : cand) {
    rollX(c);
    T[0]--;
    if (valid()) {
      ans[idx] = c;
      if (dfs(idx + 1)) {
        return true;
      }
    }
    T[0]++;
    roll_backX(c);
  }
  ans[idx] = 'X';
  return false;
}

int main() {
  while (true) {
    vector<int> t(6);
    REP(i, 6) cin >> t[i];

    REP(i, 6) T[i] = t[i];
    if (is_end())
      break;
    LEN = accumulate(t.begin(), t.end(), 0);

    int p, q;
    cin >> p >> q;
    p--;

    string answer = "Z";

    sort(t.begin(), t.end());
    do {
      REP(i, 6) T[i] = t[i];
      bool res = dfs(0);
      if (res) {
        string cand;
        REP(i, LEN) cand += ans[i];
        answer = min(answer, cand);
      }
    } while (next_permutation(t.begin(), t.end()));

    if (answer == "Z") {
      cout << "impossible" << endl;
    } else {
      for (int i = p; i < q; i++) {
        cout << answer[i];
      }
      cout << endl;
    }
  }
  return 0;
}